package com.suning.sawp.service.impl.bi.wap;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.base.Function;
import com.google.common.collect.Maps;
import com.google.gson.reflect.TypeToken;
import com.suning.framework.dal.client.DalClient;
import com.suning.nsfuaa.employee.EmployeeService;
import com.suning.nsfuaa.employee.dto.Employee;
import com.suning.rsf.consumer.ServiceLocator;
import com.suning.sawp.cache.RedisClient;
import com.suning.sawp.constants.CacheKeyConstants;
import com.suning.sawp.constants.ClerkConstants;
import com.suning.sawp.constants.ConfigConstants;
import com.suning.sawp.constants.ErrorCodeConstants;
import com.suning.sawp.dto.bi.BiSaleAmountDto;
import com.suning.sawp.dto.bi.BiStoreAvgPrice;
import com.suning.sawp.dto.bi.BiStoreBrandSaleDto;
import com.suning.sawp.dto.bi.BiStoreCateSaleCompleteRateDto;
import com.suning.sawp.dto.bi.BiStoreCateSaleDataDayDto;
import com.suning.sawp.dto.bi.BiStoreCateSaleDayDto;
import com.suning.sawp.dto.bi.BiStoreCateSaleDetailDto;
import com.suning.sawp.dto.bi.BiStoreCateSaleDto;
import com.suning.sawp.dto.bi.BiStoreJxcCateDetail;
import com.suning.sawp.dto.bi.BiStoreJxcInfo;
import com.suning.sawp.dto.bi.BiStoreSaleAmountDayDto;
import com.suning.sawp.dto.bi.BiStoreSaleBrandAvgPricedDetailsDto;
import com.suning.sawp.dto.bi.BiStoreSaleBrandAvgPricedDto;
import com.suning.sawp.dto.bi.BiStoreSaleBrandTopDto;
import com.suning.sawp.dto.bi.BiStoreSaleCateAvgPricedDetailsDto;
import com.suning.sawp.dto.bi.BiStoreSaleCateAvgPricedDto;
import com.suning.sawp.dto.bi.BiStoreSaleCateTopDto;
import com.suning.sawp.dto.bi.BiStoreSaleCompletaRateRankingDto;
import com.suning.sawp.dto.bi.BiStoreSaleCompleteDto;
import com.suning.sawp.dto.bi.BiStoreSaleCompleteRateDto;
import com.suning.sawp.dto.bi.BiStoreSaleCustAvgPricedCateDto;
import com.suning.sawp.dto.bi.BiStoreSaleCustAvgPricedDto;
import com.suning.sawp.dto.bi.BiStoreSaleCustNumDto;
import com.suning.sawp.dto.bi.BiStoreSaleCustPriceDto;
import com.suning.sawp.dto.bi.BiStoreSaleDataDayDto;
import com.suning.sawp.dto.bi.BiStoreSaleDayDto;
import com.suning.sawp.dto.bi.BiStoreSaleDetailDto;
import com.suning.sawp.dto.bi.BiStoreSaleDto;
import com.suning.sawp.dto.bi.BiStoreSaleMonthDto;
import com.suning.sawp.dto.bi.BiStoreSaleTopDto;
import com.suning.sawp.dto.bi.BiStoreYbCateDetailDto;
import com.suning.sawp.dto.bi.BiStoreYbCateInfo;
import com.suning.sawp.dto.bi.BiStoreYbDayDto;
import com.suning.sawp.dto.bi.BiStoreYbDetailDto;
import com.suning.sawp.dto.bi.BiStoreYbInfoDto;
import com.suning.sawp.dto.bi.BiStoreYbMonthDto;
import com.suning.sawp.dto.bi.BiStoreYbStaffDetailDto;
import com.suning.sawp.dto.bi.CmmdtyAvgPriceDto;
import com.suning.sawp.dto.bi.CmmdtySaleDetailDto;
import com.suning.sawp.dto.bi.CmmdtySaleDetailsDto;
import com.suning.sawp.dto.bi.TopCateDto;
import com.suning.sawp.dto.bi.sale.SaleCategory;
import com.suning.sawp.dto.common.ReturnMsg;
import com.suning.sawp.dto.mainpush.StoreManDto;
import com.suning.sawp.dto.storeman.Category;
import com.suning.sawp.intf.bi.BiBrandDaoService;
import com.suning.sawp.intf.bi.BiBrandService;
import com.suning.sawp.intf.bi.BiCmmdtyService;
import com.suning.sawp.intf.bi.BiKafkaRealTimeSaleService;
import com.suning.sawp.intf.bi.BiStoreDateService;
import com.suning.sawp.intf.bi.StoreGuideBiService;
import com.suning.sawp.po.bi.BiBrandSale;
import com.suning.sawp.po.bi.BiStoreCateMonSaleTask;
import com.suning.sawp.po.bi.BiStoreMonSaleTask;
import com.suning.sawp.service.util.DJStringUtils;
import com.suning.sawp.service.util.DateUtils;
import com.suning.sawp.service.util.GsonUtils;

/**
 * 门店店长，督导数据统计服务实现 〈一句话功能简述〉<br>
 * 〈功能详细描述〉
 *
 * @author 15050519
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Service
public class BiStoreDateServiceImpl implements BiStoreDateService {

    /**
     * 除数保留2位小数
     */
    public static final int SCALE_TWO = 2;

    /**
     * 日志
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(BiStoreDateServiceImpl.class);

    /**
     * 门店月度品类品类销冠缓存
     */
    private static final String STORE_TOP_SALE_PREFIX = "STORE_MONTH_SALE_TOP_%s_%s";

    /**
     * 第一个
     */
    private static final int FIRST_ELE = 0;

    /**
     * 按销售额排序
     */
    private static final String ORDER_BY_AMOUNT = "0";
    
    /**
     * 为空销售员工号
     */
    private static final String DJYB = "DJYB";
    
    
    /**
     * 为空销售员名称
     */
    private static final String DJYB_NAME = "工号为空";
    
    /**
     * 日期长度
     */
    private static final int DAY_DATE_LENGTH = 10;
    
    @Autowired
    DalClient dalClient;

    @Autowired
    RedisClient redisClient;

    /**
     * BI实时销售数据接口服务
     */
    @Autowired
    BiKafkaRealTimeSaleService biKafkaRealTimeSaleService;

    @Resource
    BiBrandDaoService biBrandDaoService;

    @Resource
    BiBrandService biBrandService;
    
    @Resource
    StoreGuideBiService storeGuideBiService;
    
    @Resource
    BiCmmdtyService biCmmdtyService;
    
    private EmployeeService employeeService = ServiceLocator.getService(EmployeeService.class, "");
    
    /**
     * 单品RSF接口服务
     */
    com.suning.nsfdas.service.CmmdtySaleService cmmdtySaleService = ServiceLocator.getService(
            com.suning.nsfdas.service.CmmdtySaleService.class, null);
    
    /**
     * 品牌RSF接口服务
     */
    com.suning.nsfdas.service.BrandSaleService brandSaleService = ServiceLocator.getService(
            com.suning.nsfdas.service.BrandSaleService.class, null);

    /**
     * 查询店长日销售数据 功能描述: <br>
     * 〈功能详细描述〉
     *
     * @param staffId
     * @param storeCode
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @Override
    public BiStoreSaleDto queryKeeperDataByDay(String staffId, String storeCode) {
        LOGGER.info("enter queryKeeperDataByDay, staffId={}, storeCode={}", staffId, storeCode);
        BiStoreSaleDto storeSale = new BiStoreSaleDto();
        // 取当月的销售任务金额，毛利任务金额
        String month = DateUtils.getThisMonth();
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("month", month);
        paramMap.put("storeCode", storeCode);
        BiStoreMonSaleTask task = dalClient.queryForObject("BI_STORE_DATA_WAP.QUERY_STORE_TASK_TARGET", paramMap,
                BiStoreMonSaleTask.class);
        LOGGER.debug("queryKeeperDataByDay task={}", task);
        // 门店任务目标
        if (null == task || null == task.getTaskSaleAmount()) {
            storeSale.setTaskSaleAmount(BiSaleAmountDto.zero());
        } else {
            storeSale.setTaskSaleAmount(BiSaleAmountDto.valueOf(task.getTaskSaleAmount()));
        }
        // 门店毛利任务目标
        if (null == task || null == task.getTaskGrossProfit()) {
            storeSale.setTaskGrossProfit(BiSaleAmountDto.zero());
        } else {
            storeSale.setTaskGrossProfit(BiSaleAmountDto.valueOf(task.getTaskGrossProfit()));
        }
        // 取门店实时数据
        Map<String, Object> storeSaleRedis = biKafkaRealTimeSaleService.getStoreSaleDataFromRedis(storeCode);
        BigDecimal saleAmount;
        if (null != storeSaleRedis && null != storeSaleRedis.get("payAmt")) {
            saleAmount = DJStringUtils.bigDecimal(String.valueOf(storeSaleRedis.get("payAmt")));
        } else {
            saleAmount = BigDecimal.ZERO;
        }
        storeSale.setSaleAmount(BiSaleAmountDto.valueOf(saleAmount));

        // 门店去年同期销售金额
        BigDecimal lasYearSaleAmount = biKafkaRealTimeSaleService.queryLastYearStoreSaleAmount(storeCode);
        // 销售同比
        storeSale.setSaleRate(DJStringUtils.calculateYoyString(saleAmount, lasYearSaleAmount));

        // 计算目标完成率
        if (null != task && null != task.getTaskSaleAmount()) {
            BigDecimal monthAmount;
            // 月销售数据
            BiStoreSaleMonthDto monthSale = dalClient.queryForObject("BI_STORE_DATA_WAP.QUERY_STORE_SALE_MONTH",
                    paramMap, BiStoreSaleMonthDto.class);
            LOGGER.debug("queryKeeperDataByDay monthSale={}", monthSale);
            if (null != monthSale && StringUtils.isNotBlank(monthSale.getSaleAmount())) {
                monthAmount = DJStringUtils.bigDecimal(monthSale.getSaleAmount());
            } else {
                monthAmount = BigDecimal.ZERO;
            }
            // 月度总销售额
            monthAmount = monthAmount.add(saleAmount);
            storeSale.setCompleteRate(DJStringUtils.calculateRateString(monthAmount.toString(), task
                    .getTaskSaleAmount().toString()));
        } else {
            // 没有目标,设置0
            storeSale.setCompleteRate("0.0");
        }

        // 当前系统时间
        storeSale.setSystemTime(DateUtils.getNowLongStr());

        // 今日客单价
        BiStoreSaleCustPriceDto custPrice = new BiStoreSaleCustPriceDto();
        BigDecimal custCnt;
        if (null != storeSaleRedis && null != storeSaleRedis.get("memberCount")) {
            custCnt = DJStringUtils.bigDecimal(String.valueOf(storeSaleRedis.get("memberCount")));
        } else {
            custCnt = BigDecimal.ZERO;
        }
        BigDecimal custPriceReal = DJStringUtils.divide(saleAmount, custCnt);
        // 设置实时客单价
        custPrice.setCurrCustAvgPrice(BiSaleAmountDto.valueOfLow(custPriceReal));

        // 初始化8个品类，整理入参
        List<String> cates = new ArrayList<String>();
        cates.addAll(ConfigConstants.DEFAULT_CATE_MAP.keySet());
        paramMap.put("cates", cates);
        // 昨日
        paramMap.put("day", DateUtils.getNextDayStr(-1, DateUtils.YYYYMMDD_PATTERN));
        // 昨日客单价, 品类销冠, 品牌销冠
        BiStoreSaleTopDto topSale = dalClient.queryForObject("BI_STORE_DATA_WAP.QUERY_STORE_CUST_AVG_SALE_TOP_DAY",
                paramMap, BiStoreSaleTopDto.class);
        LOGGER.debug("queryKeeperDataByDay topSale1={}", topSale);

        // 昨日客单价
        if (null == topSale || StringUtils.isBlank(topSale.getCustAvgPrice())) {
            // 昨日客单价
            custPrice.setLastCustAvgPrice(BiSaleAmountDto.valueOfLow(BigDecimal.ZERO));
        } else {
            // 昨日客单价
            custPrice
                    .setLastCustAvgPrice(BiSaleAmountDto.valueOfLow(DJStringUtils.bigDecimal(topSale.getCustAvgPrice())));
        }

        // 查询同期客单价
        BigDecimal custPriceLastYear = biKafkaRealTimeSaleService.queryLastYearStoreCustAvgPrice(storeCode);
        // 客单价同比
        custPrice.setSaleRate(DJStringUtils.calculateYoyString(custPriceReal, custPriceLastYear));

        // 客单价
        storeSale.setCustAvgPirce(custPrice);

        // 今日品类销冠
        BiStoreSaleCateTopDto cateTop = new BiStoreSaleCateTopDto();
        BiStoreCateSaleDto currCateSale = new BiStoreCateSaleDto();
        // 查询品类销冠数据
        Map<String, Object> cateSaleTop = getStoreCateSaleTop(storeCode);
        if (null != cateSaleTop && null != cateSaleTop.get("payAmt")) {
            // deptCd
            currCateSale.setCateCode(String.valueOf(cateSaleTop.get("deptCd")));
            currCateSale.setCateName(ConfigConstants.DEFAULT_CATE_MAP.get(String.valueOf(cateSaleTop.get("deptCd"))));
            currCateSale.setSaleAmount(BiSaleAmountDto.valueOf(String.valueOf(cateSaleTop.get("payAmt"))));
        } else {
            currCateSale.setCateCode("");
            currCateSale.setCateName("");
            currCateSale.setSaleAmount(BiSaleAmountDto.zero());
        }
        cateTop.setCurrSale(currCateSale);
        topSale = dalClient.queryForObject("BI_STORE_DATA_WAP.QUERY_STORE_CATE_SALE_TOP_DAY", paramMap,
                BiStoreSaleTopDto.class);
        LOGGER.debug("queryKeeperDataByDay topSale2={}", topSale);
        // 昨日品类销冠
        BiStoreCateSaleDto lastCateSale = new BiStoreCateSaleDto();
        if (null == topSale || StringUtils.isBlank(topSale.getCateCode())) {
            lastCateSale.setCateCode("");
            lastCateSale.setCateName("");
            lastCateSale.setSaleAmount(BiSaleAmountDto.zero());
        } else {
            lastCateSale.setCateCode(topSale.getCateCode());
            lastCateSale.setCateName(ConfigConstants.DEFAULT_CATE_MAP.get(topSale.getCateCode()));
            lastCateSale.setSaleAmount(BiSaleAmountDto.valueOf(DJStringUtils.bigDecimal(topSale.getCateSaleAmount())));
        }
        // 品类销冠
        cateTop.setLastSale(lastCateSale);
        storeSale.setCateTopSale(cateTop);

        // 今日品牌销冠
        BiStoreSaleBrandTopDto brandTop = new BiStoreSaleBrandTopDto();
        // 查询当日品牌实时销冠数据
        Map<String, Object> brandSaleTop = getStoreBrandSaleTop(storeCode);
        BiStoreBrandSaleDto currBrandSale = new BiStoreBrandSaleDto();
        // 不为空时
        if (null != brandSaleTop && null != brandSaleTop.get("payAmt")) {
            // brandCd
            currBrandSale.setBrandCode(String.valueOf(brandSaleTop.get("brandCd")));
            currBrandSale.setBrandName(String.valueOf(brandSaleTop.get("brandName")));
            currBrandSale.setSaleAmount(BiSaleAmountDto.valueOf(String.valueOf(brandSaleTop.get("payAmt"))));
        } else {
            currBrandSale.setBrandCode("");
            currBrandSale.setBrandName("");
            currBrandSale.setSaleAmount(BiSaleAmountDto.zero());
        }
        brandTop.setCurrSale(currBrandSale);

        topSale = dalClient.queryForObject("BI_STORE_DATA_WAP.QUERY_STORE_BRAND_SALE_TOP_DAY", paramMap,
                BiStoreSaleTopDto.class);
        LOGGER.debug("queryKeeperDataByDay topSale={}", topSale);
        // 昨日品牌销冠
        BiStoreBrandSaleDto lastBrandSale = new BiStoreBrandSaleDto();
        if (null == topSale || StringUtils.isBlank(topSale.getBrandCode())) {
            lastBrandSale.setBrandCode("");
            lastBrandSale.setBrandName("");
            lastBrandSale.setSaleAmount(BiSaleAmountDto.zero());
        } else {
            lastBrandSale.setBrandCode(topSale.getBrandCode());
            lastBrandSale.setBrandName(topSale.getBrandName());
            lastBrandSale
                    .setSaleAmount(BiSaleAmountDto.valueOf(DJStringUtils.bigDecimal(topSale.getBrandSaleAmount())));
        }
        // 品牌销冠
        brandTop.setLastSale(lastBrandSale);
        storeSale.setBrandTopSale(brandTop);

        // 计算前6日趋势图
        List<String> days = new ArrayList<String>();
        // 前6天的日期
        for (int i = 6; i > 0; i--) {
            days.add(DateUtils.getNextDayStr(-i, DateUtils.YYYYMMDD_PATTERN));
        }
        paramMap.put("days", days);
        // 查询前6条销售数据
        List<BiStoreSaleAmountDayDto> saleDays = dalClient.queryForList("BI_STORE_DATA_WAP.QUERY_STORE_AMOUNT_DAYS",
                paramMap, BiStoreSaleAmountDayDto.class);
        LOGGER.debug("queryKeeperDataByDay saleDays={}", saleDays);
        if (CollectionUtils.isEmpty(saleDays)) {
            saleDays = new ArrayList<BiStoreSaleAmountDayDto>();
        }
        // 处理趋势数据
        Map<String, BiStoreSaleAmountDayDto> saleMap = Maps.uniqueIndex(saleDays,
                new Function<BiStoreSaleAmountDayDto, String>() {
                    @Override
                    public String apply(BiStoreSaleAmountDayDto from) {
                        return from.getSaleDate();
                    }
                });
        List<BiStoreSaleAmountDayDto> saleout = new ArrayList<BiStoreSaleAmountDayDto>();
        BiStoreSaleAmountDayDto saleDay;
        // 循环每一天
        for (String day : days) {
            saleDay = saleMap.get(day);
            // 没有数据时,默认为0
            if (null == saleDay) {
                saleDay = new BiStoreSaleAmountDayDto();
                saleDay.setSaleDate(day);
                saleDay.setSaleAmount("0.00");
                saleDay.setLastYearSaleAmount("0.00");
            } else if (StringUtils.isBlank(saleDay.getSaleAmount())) {
                saleDay.setSaleAmount("0.00");
            } else if (StringUtils.isBlank(saleDay.getLastYearSaleAmount())) {
                saleDay.setLastYearSaleAmount("0.00");
            }
            // 转换成万元单位
            saleDay.setSaleAmount(BiSaleAmountDto.valueOf(DJStringUtils.bigDecimal(saleDay.getSaleAmount()),
                    BiSaleAmountDto.UNIT_WAN).getAmount());
            saleDay.setLastYearSaleAmount(BiSaleAmountDto.valueOf(
                    DJStringUtils.bigDecimal(saleDay.getLastYearSaleAmount()), BiSaleAmountDto.UNIT_WAN).getAmount());
            // 转换成星期
            saleDay.setWeek("周" + DateUtils.getWeekOfDate(DateUtils.getDateByStr(day, DateUtils.YYYYMMDD_PATTERN)));
            saleout.add(saleDay);
        }
        // 增加今天的销售数据
        BiStoreSaleAmountDayDto todaySale = new BiStoreSaleAmountDayDto();
        todaySale.setSaleDate(DateUtils.getNextDayStr(0, DateUtils.YYYYMMDD_PATTERN));
        // 转换成万元单位
        todaySale.setSaleAmount(BiSaleAmountDto.valueOf(saleAmount, BiSaleAmountDto.UNIT_WAN).getAmount());
        todaySale.setLastYearSaleAmount(BiSaleAmountDto.valueOf(lasYearSaleAmount, BiSaleAmountDto.UNIT_WAN)
                .getAmount());
        // 转换成星期
        todaySale.setWeek("周"
                + DateUtils.getWeekOfDate(DateUtils.getDateByStr(todaySale.getSaleDate(), DateUtils.YYYYMMDD_PATTERN)));
        saleout.add(todaySale);

        // 设置趋势
        storeSale.setSaleList(saleout);
        
        // 设置平均价
        BigDecimal currCommAvgPrice = BigDecimal.ZERO;
        BigDecimal lastCommAvgPrice = BigDecimal.ZERO;
        String saleCnt = "0";
        String saleRate = "0.0";
        if (null != storeSaleRedis && null != storeSaleRedis.get("saleCnt")) {
            saleCnt = String.valueOf(storeSaleRedis.get("saleCnt"));
        }
        currCommAvgPrice = DJStringUtils.divide(saleAmount, DJStringUtils.bigDecimal(saleCnt));
        paramMap.clear();
        paramMap.put("storeCode", storeCode);
        paramMap.put("day", DateUtils.getNextDayStr(-1, DateUtils.YYYYMMDD_PATTERN));
        BiStoreSaleDataDayDto saleDataYesterday = dalClient.queryForObject("BI_STORE_DATA_WAP.QUERY_STORE_DATA_BY_DAY",
                paramMap, BiStoreSaleDataDayDto.class);
        if (null != saleDataYesterday && StringUtils.isNotBlank(saleDataYesterday.getSaleAmount())
                && StringUtils.isNotBlank(saleDataYesterday.getSaleVolume())) {
            lastCommAvgPrice = DJStringUtils.divide(DJStringUtils.bigDecimal(saleDataYesterday.getSaleAmount()),
                    DJStringUtils.bigDecimal(saleDataYesterday.getSaleVolume()));
        }
        // 同比是去年的销售数据
        // 门店去年同期销售金额
        Map<String, Object> lastYearSaleData = biKafkaRealTimeSaleService.queryLastYearStoreSaleData(storeCode);
        if (null != lastYearSaleData && null != lastYearSaleData.get("SALE_AMOUNT")
                && null != lastYearSaleData.get("SALE_VOLUME")) {
            BigDecimal lastYearCommAvgPrice = DJStringUtils.divide(
                    DJStringUtils.bigDecimal(String.valueOf(lastYearSaleData.get("SALE_AMOUNT"))),
                    DJStringUtils.bigDecimal(String.valueOf(lastYearSaleData.get("SALE_VOLUME"))));
            saleRate = DJStringUtils.calculateYoyString(currCommAvgPrice, lastYearCommAvgPrice);
        }

        storeSale.setCommAvgPrice(new BiStoreAvgPrice(BiSaleAmountDto.valueOfLow(currCommAvgPrice), BiSaleAmountDto
                .valueOfLow(lastCommAvgPrice), saleRate));
				
				
		//设置电器延保
        BiStoreYbInfoDto biStoreYbInfoDto = new BiStoreYbInfoDto();
        
        BigDecimal ybAmount = biKafkaRealTimeSaleService.getStoreYbSaleFromRedis(storeCode);
        //门店延保销售额
        biStoreYbInfoDto.setSaleAmount(BiSaleAmountDto.valueOf(ybAmount));
        biStoreYbInfoDto.setSaleRate( DJStringUtils.calculateRateString(ybAmount.toString(), saleAmount.toString()));

        storeSale.setYbInfo(biStoreYbInfoDto);
        
        // 销售top单品信息
        storeSale.setTopSaleCmmdty(biCmmdtyService.queryTopSaleCmmdtyByDay(storeCode, null,
                DateUtils.getDateStrByFormat(null, DateUtils.YYYYMMDD_PATTERN)));

        return storeSale;
    }

    /**
     * 查询门店月度实时数据 功能描述: <br>
     * 〈功能详细描述〉
     *
     * @param storeCode
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @Override
    public BiStoreSaleDto queryKeeperDataByMonth(String storeCode) {
        LOGGER.info("enter queryKeeperDataByMonth, storeCode={}", storeCode);
        BiStoreSaleDto storeSale = new BiStoreSaleDto();
        // 取门店实时数据
        Map<String, Object> storeSaleRedis = biKafkaRealTimeSaleService.getStoreSaleDataFromRedis(storeCode);
        BigDecimal saleAmount;
        // 取今日的实时销售数据
        if (null != storeSaleRedis && null != storeSaleRedis.get("payAmt")) {
            saleAmount = DJStringUtils.bigDecimal(String.valueOf(storeSaleRedis.get("payAmt")));
        } else {
            saleAmount = BigDecimal.ZERO;
        }

        // 查询本月的销售,上月的销售数据
        String month = DateUtils.getThisMonth();
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("month", month);
        paramMap.put("storeCode", storeCode);
        BiStoreSaleMonthDto monthSale = dalClient.queryForObject("BI_STORE_DATA_WAP.QUERY_STORE_SALE_MONTH", paramMap,
                BiStoreSaleMonthDto.class);
        LOGGER.debug("queryKeeperDataByMonth monthSale={}", monthSale);
        // 当月销售额
        if (null != monthSale && StringUtils.isNotBlank(monthSale.getSaleAmount())) {
            saleAmount = saleAmount.add(DJStringUtils.bigDecimal(monthSale.getSaleAmount()));
        }
        storeSale.setSaleAmount(BiSaleAmountDto.valueOf(saleAmount));
        // 查询门店目标销售额
        BiStoreMonSaleTask task = dalClient.queryForObject("BI_STORE_DATA_WAP.QUERY_STORE_TASK_TARGET", paramMap,
                BiStoreMonSaleTask.class);
        LOGGER.debug("queryKeeperDataByMonth task={}", task);

        // 门店任务目标
        if (null == task || null == task.getTaskSaleAmount()) {
            storeSale.setTaskSaleAmount(BiSaleAmountDto.zero());
        } else {
            storeSale.setTaskSaleAmount(BiSaleAmountDto.valueOf(task.getTaskSaleAmount()));
        }
        // 门店毛利任务目标
        if (null == task || null == task.getTaskGrossProfit()) {
            storeSale.setTaskGrossProfit(BiSaleAmountDto.zero());
        } else {
            storeSale.setTaskGrossProfit(BiSaleAmountDto.valueOf(task.getTaskGrossProfit()));
        }

        // 门店任务目标
        if (null != task && null != task.getTaskSaleAmount()) {
            storeSale.setCompleteRate(DJStringUtils.calculateRateString(saleAmount.toString(), task.getTaskSaleAmount()
                    .toString()));
        } else {
            // 默认完成率
            storeSale.setCompleteRate("0.0");
        }

        // 门店去年同期销售金额
        BigDecimal lasYearSaleAmount;
        // 初始化
        if (null != monthSale && StringUtils.isNotBlank(monthSale.getLastYearSale())) {
            lasYearSaleAmount = DJStringUtils.bigDecimal(monthSale.getLastYearSale());
        } else {
            lasYearSaleAmount = BigDecimal.ZERO;
        }
        // 销售同比
        storeSale.setSaleRate(DJStringUtils.calculateYoyString(saleAmount, lasYearSaleAmount));

        // 月初
        if ("01".equals(DateUtils.getDateStrByFormat(new Date(), "dd"))) {
            storeSale.setSystemTime("暂未统计");
        } else {
            // 昨日时间
            storeSale.setSystemTime("截至" + DateUtils.getDateStrByFormat(DateUtils.getNextDay(new Date(), -1), "MM-dd"));
        }

        // 客户数
        BiStoreSaleCustNumDto custNumDto = new BiStoreSaleCustNumDto();
        String custNum;
        String lastCustNum;
        // 判断客户数量是否为空
        if (null != monthSale && StringUtils.isNotBlank(monthSale.getCustNum())) {
            custNum = monthSale.getCustNum();
        } else {
            custNum = "0";
        }
        // 判断同期客户数量是否为空
        if (null != monthSale && StringUtils.isNotBlank(monthSale.getLastYearCustNum())) {
            lastCustNum = monthSale.getLastYearCustNum();
        } else {
            lastCustNum = "0";
        }
        custNumDto.setCurrCustNum(custNum);
        custNumDto.setLastCustNum(lastCustNum);
        // 计算客户同比
        custNumDto.setSaleRate(DJStringUtils.calculateYoyString(DJStringUtils.bigDecimal(custNum),
                DJStringUtils.bigDecimal(lastCustNum)));
        storeSale.setCustNum(custNumDto);

        // 本月客单价
        BiStoreSaleCustPriceDto custPrice = new BiStoreSaleCustPriceDto();
        BigDecimal custPriceMonth;
        BigDecimal custPriceLastYear;
        // 本月客单价
        if (null != monthSale && StringUtils.isNotBlank(monthSale.getCustAvgPrice())) {
            custPriceMonth = DJStringUtils.bigDecimal(monthSale.getCustAvgPrice());
        } else {
            custPriceMonth = BigDecimal.ZERO;
        }
        custPrice.setCurrCustAvgPrice(BiSaleAmountDto.valueOfLow(custPriceMonth));

        // 去年同期客单价
        if (null != monthSale && StringUtils.isNotBlank(monthSale.getLastYearCustAvgPrice())) {
            custPriceLastYear = DJStringUtils.bigDecimal(monthSale.getLastYearCustAvgPrice());
        } else {
            custPriceLastYear = BigDecimal.ZERO;
        }
        custPrice.setLastCustAvgPrice(BiSaleAmountDto.valueOfLow(custPriceLastYear));

        // 客单价同比
        custPrice.setSaleRate(DJStringUtils.calculateYoyString(custPriceMonth, custPriceLastYear));

        // 客单价数据
        storeSale.setCustAvgPirce(custPrice);

        // 处理销冠数
        BiStoreSaleCateTopDto cateTop = new BiStoreSaleCateTopDto();
        BiStoreCateSaleDto currCateSale = new BiStoreCateSaleDto();
        BiStoreCateSaleDto lastCateSale = new BiStoreCateSaleDto();
        BiStoreSaleBrandTopDto brandTop = new BiStoreSaleBrandTopDto();
        BiStoreBrandSaleDto currBrandSale = new BiStoreBrandSaleDto();
        BiStoreBrandSaleDto lastBrandSale = new BiStoreBrandSaleDto();
        // 查询本月的品类品牌销冠
        BiStoreSaleTopDto currStoreSaleTop = queryStoreSaleTopMonth(storeCode, month);
        // 本月品类销冠
        if (null != currStoreSaleTop && StringUtils.isNotBlank(currStoreSaleTop.getCateCode())) {
            currCateSale.setCateCode(currStoreSaleTop.getCateCode());
            currCateSale.setCateName(ConfigConstants.DEFAULT_CATE_MAP.get(currStoreSaleTop.getCateCode()));
            currCateSale.setSaleAmount(BiSaleAmountDto.valueOf(DJStringUtils.bigDecimal(currStoreSaleTop
                    .getCateSaleAmount())));
        } else {
            currCateSale.setCateCode("");
            currCateSale.setCateName("");
            currCateSale.setSaleAmount(BiSaleAmountDto.zero());
        }
        cateTop.setCurrSale(currCateSale);
        // 本月品牌销冠
        if (null != currStoreSaleTop && StringUtils.isNotBlank(currStoreSaleTop.getBrandCode())) {
            currBrandSale.setBrandCode(currStoreSaleTop.getBrandCode());
            currBrandSale.setBrandName(currStoreSaleTop.getBrandName());
            currBrandSale.setSaleAmount(BiSaleAmountDto.valueOf(DJStringUtils.bigDecimal(currStoreSaleTop
                    .getBrandSaleAmount())));
        } else {
            currBrandSale.setBrandCode("");
            currBrandSale.setBrandName("");
            currBrandSale.setSaleAmount(BiSaleAmountDto.zero());
        }
        brandTop.setCurrSale(currBrandSale);

        // 查询同期月的品类品牌销冠
        BiStoreSaleTopDto lastStoreSaleTop = queryStoreLastYearSaleTopMonth(storeCode,
                DateUtils.getDateStrByFormat(DateUtils.getNextYear(new Date(), -1), "yyyyMM"));
        LOGGER.debug("queryKeeperDataByMonth lastStoreSaleTop={}", lastStoreSaleTop);
        // 同期月品类销冠
        if (null != lastStoreSaleTop && StringUtils.isNotBlank(lastStoreSaleTop.getCateCode())) {
            lastCateSale.setCateCode(lastStoreSaleTop.getCateCode());
            lastCateSale.setCateName(ConfigConstants.DEFAULT_CATE_MAP.get(lastStoreSaleTop.getCateCode()));
            lastCateSale.setSaleAmount(BiSaleAmountDto.valueOf(DJStringUtils.bigDecimal(lastStoreSaleTop
                    .getCateSaleAmount())));
        } else {
            lastCateSale.setCateCode("");
            lastCateSale.setCateName("");
            lastCateSale.setSaleAmount(BiSaleAmountDto.zero());
        }
        cateTop.setLastSale(lastCateSale);
        // 同期月品牌销冠
        if (null != lastStoreSaleTop && StringUtils.isNotBlank(lastStoreSaleTop.getBrandCode())) {
            lastBrandSale.setBrandCode(lastStoreSaleTop.getBrandCode());
            lastBrandSale.setBrandName(lastStoreSaleTop.getBrandName());
            lastBrandSale.setSaleAmount(BiSaleAmountDto.valueOf(DJStringUtils.bigDecimal(lastStoreSaleTop
                    .getBrandSaleAmount())));
        } else {
            lastBrandSale.setBrandCode("");
            lastBrandSale.setBrandName("");
            lastBrandSale.setSaleAmount(BiSaleAmountDto.zero());
        }
        brandTop.setLastSale(lastBrandSale);

        // 设置本月，同期月品类品牌销冠数据
        storeSale.setCateTopSale(cateTop);
        storeSale.setBrandTopSale(brandTop);

        // 月份销售趋势图
        // 计算前5个月趋势图
        List<String> months = new ArrayList<String>();
        // 前5天的月份
        for (int i = 5; i > 0; i--) {
            months.add(DateUtils.getMonth(-i));
        }
        paramMap.put("months", months);
        // 查询前5个月销售数据
        List<BiStoreSaleAmountDayDto> saleMonths = dalClient.queryForList(
                "BI_STORE_DATA_WAP.QUERY_STORE_AMOUNT_MONTHS", paramMap, BiStoreSaleAmountDayDto.class);
        LOGGER.debug("queryKeeperDataByMonth saleMonths={}", saleMonths);
        if (CollectionUtils.isEmpty(saleMonths)) {
            saleMonths = new ArrayList<BiStoreSaleAmountDayDto>();
        }
        // 处理趋势数据
        Map<String, BiStoreSaleAmountDayDto> saleMap = Maps.uniqueIndex(saleMonths,
                new Function<BiStoreSaleAmountDayDto, String>() {
                    @Override
                    public String apply(BiStoreSaleAmountDayDto from) {
                        return from.getSaleDate();
                    }
                });
        List<BiStoreSaleAmountDayDto> saleout = new ArrayList<BiStoreSaleAmountDayDto>();
        BiStoreSaleAmountDayDto saleMonth;
        // 循环每一月
        for (String monthOld : months) {
            saleMonth = saleMap.get(monthOld);
            // 没有数据时,默认为0
            if (null == saleMonth) {
                saleMonth = new BiStoreSaleAmountDayDto();
                saleMonth.setSaleDate(monthOld);
                saleMonth.setSaleAmount("0.00");
                saleMonth.setLastYearSaleAmount("0.00");
            } else if (StringUtils.isBlank(saleMonth.getSaleAmount())) {
                saleMonth.setSaleAmount("0.00");
            } else if (StringUtils.isBlank(saleMonth.getLastYearSaleAmount())) {
                saleMonth.setLastYearSaleAmount("0.00");
            }
            // 转换成万元单位
            saleMonth.setSaleAmount(BiSaleAmountDto.valueOf(DJStringUtils.bigDecimal(saleMonth.getSaleAmount()),
                    BiSaleAmountDto.UNIT_WAN).getAmount());
            saleMonth.setLastYearSaleAmount(BiSaleAmountDto.valueOf(
                    DJStringUtils.bigDecimal(saleMonth.getLastYearSaleAmount()), BiSaleAmountDto.UNIT_WAN).getAmount());
            saleout.add(saleMonth);
        }
        // 增加本月的销售数据
        BiStoreSaleAmountDayDto todaySale = new BiStoreSaleAmountDayDto();
        todaySale.setSaleDate(month);
        // 转换成万元单位
        todaySale.setSaleAmount(BiSaleAmountDto.valueOf(saleAmount, BiSaleAmountDto.UNIT_WAN).getAmount());
        todaySale.setLastYearSaleAmount(BiSaleAmountDto.valueOf(lasYearSaleAmount, BiSaleAmountDto.UNIT_WAN)
                .getAmount());
        saleout.add(todaySale);
        // 设置趋势
        storeSale.setSaleList(saleout);
        
        // 平均单价
        BigDecimal currCommAvgPrice = BigDecimal.ZERO;
        BigDecimal lastCommAvgPrice = BigDecimal.ZERO;
        BigDecimal saleAmountEx = BigDecimal.ZERO;
        BigDecimal saleCnt = BigDecimal.ZERO;
        String saleRate = "0.0";
        if (null != monthSale && StringUtils.isNotBlank(monthSale.getSaleAmount())) {
            saleAmountEx = DJStringUtils.bigDecimal(monthSale.getSaleAmount());
        }
        if (null != monthSale && StringUtils.isNotBlank(monthSale.getSaleVolume())) {
            saleCnt = DJStringUtils.bigDecimal(monthSale.getSaleVolume());
        }
        currCommAvgPrice = DJStringUtils.divide(saleAmountEx, saleCnt);
        if (null != monthSale && StringUtils.isNotBlank(monthSale.getLastYearSale())
                && StringUtils.isNotBlank(monthSale.getLastYearVolume())) {
            lastCommAvgPrice = DJStringUtils.divide(DJStringUtils.bigDecimal(monthSale.getLastYearSale()),
                    DJStringUtils.bigDecimal(monthSale.getLastYearVolume()));
        }
        saleRate = DJStringUtils.calculateYoyString(currCommAvgPrice, lastCommAvgPrice);
        storeSale.setCommAvgPrice(new BiStoreAvgPrice(BiSaleAmountDto.valueOfLow(currCommAvgPrice), BiSaleAmountDto
                .valueOfLow(lastCommAvgPrice), saleRate));
        
  
        //设置电器延保
        BiStoreYbInfoDto biStoreYbInfoDto = new BiStoreYbInfoDto();
        BigDecimal ybAmount = biKafkaRealTimeSaleService.getStoreYbSaleFromRedis(storeCode);
        
        Map<String, Object> ybParamMap = new HashMap<String, Object>();
        ybParamMap.put("month", month);
        ybParamMap.put("storeCode", storeCode);
        try {
            BiStoreYbMonthDto ybMonthSale = dalClient.queryForObject("BI_STORE_YB.QUERY_STORE_YB_MONTH", ybParamMap,
                    BiStoreYbMonthDto.class);
            LOGGER.info("queryKeeperDataByMonth ybMonthSale={}", ybMonthSale);
            // 当月销售额
            if (null != ybMonthSale && StringUtils.isNotBlank(ybMonthSale.getYbSaleAmount())) {
                ybAmount = ybAmount.add(DJStringUtils.bigDecimal(ybMonthSale.getYbSaleAmount()));
            }
        } catch (Exception e) {
            LOGGER.error("queryKeeperDataByMonth error storeCode:"+ storeCode + ",month" + month ,e );
        }
        
        //门店延保销售额
        biStoreYbInfoDto.setSaleAmount(BiSaleAmountDto.valueOf(ybAmount));
        biStoreYbInfoDto.setSaleRate( DJStringUtils.calculateRateString(ybAmount.toString(), saleAmount.toString()));
        storeSale.setYbInfo(biStoreYbInfoDto);
        
        // 销售top单品信息
        storeSale.setTopSaleCmmdty(biCmmdtyService.queryTopSaleCmmdtyByMonth(storeCode, null,
                DateUtils.getDateStrByFormat(null, DateUtils.YYYYMM_PATTERN)));

        // 返回对象
        return storeSale;
    }

    /**
     * 查询门店实时品类销售冠军 功能描述: <br>
     * 〈功能详细描述〉
     *
     * @param storeCode
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    private Map<String, Object> getStoreCateSaleTop(String storeCode) {
        // 查询门店品类实时销售数据
        List<Map<String, Object>> cateSales = biKafkaRealTimeSaleService.getStoreCateSaleDataFromRedis(storeCode);
        // 数据不为空时
        if (CollectionUtils.isNotEmpty(cateSales)) {
            // 根据销售额排序
            Collections.sort(cateSales, new Comparator<Map<String, Object>>() {
                @Override
                public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                    if (null != o2.get("payAmt") && null != o1.get("payAmt")) {
                        return DJStringUtils.bigDecimal(String.valueOf(o2.get("payAmt"))).compareTo(
                                DJStringUtils.bigDecimal(String.valueOf(o1.get("payAmt"))));
                    } else if (null == o2.get("payAmt")) {
                        return -1;
                    } else if (null == o1.get("payAmt")) {
                        return 1;
                    }
                    // 降序
                    return 0;
                }
            });
            return cateSales.get(0);
        }
        return null;
    }

    /**
     * 查询门店实时品牌销售冠军 功能描述: <br>
     * 〈功能详细描述〉
     *
     * @param storeCode
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    private Map<String, Object> getStoreBrandSaleTop(String storeCode) {
        // 查询门店品牌实时销售数据
        List<Map<String, Object>> cateSales = biKafkaRealTimeSaleService.getStoreBrandSaleDataFromRedis(storeCode);
        // 数据不为空时
        if (CollectionUtils.isNotEmpty(cateSales)) {
            // 根据销售额排序
            Collections.sort(cateSales, new Comparator<Map<String, Object>>() {
                @Override
                public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                    if (null != o2.get("payAmt") && null != o1.get("payAmt")) {
                        return DJStringUtils.bigDecimal(String.valueOf(o2.get("payAmt"))).compareTo(
                                DJStringUtils.bigDecimal(String.valueOf(o1.get("payAmt"))));
                    } else if (null == o2.get("payAmt")) {
                        return -1;
                    } else if (null == o1.get("payAmt")) {
                        return 1;
                    }
                    // 降序
                    return 0;
                }
            });
            return cateSales.get(0);
        }
        return null;
    }

    /**
     * 查询门店品类，品牌月度销冠数据 功能描述: <br>
     * 本月不走缓存,其他月可走缓存 〈功能详细描述〉
     *
     * @param storeCode
     * @param month
     * @param isCache
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    private BiStoreSaleTopDto queryStoreSaleTopMonth(String storeCode, String month) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("storeCode", storeCode);
        paramMap.put("month", month);
        // 品类销冠
        BiStoreSaleTopDto cateTopSale = dalClient.queryForObject("BI_STORE_DATA_WAP.QUERY_STORE_CATE_SALE_TOP_MONTH",
                paramMap, BiStoreSaleTopDto.class);

        // 品牌销冠
        BiStoreSaleTopDto brandTopSale = dalClient.queryForObject("BI_STORE_DATA_WAP.QUERY_STORE_BRAND_SALE_TOP_MONTH",
                paramMap, BiStoreSaleTopDto.class);

        // 存在数据时
        if (null != cateTopSale || null != brandTopSale) {
            BiStoreSaleTopDto top = new BiStoreSaleTopDto();
            // 存在品类销冠
            if (null != cateTopSale) {
                top.setCateCode(cateTopSale.getCateCode());
                top.setCateSaleAmount(cateTopSale.getCateSaleAmount());
                top.setCateSaleVolume(cateTopSale.getCateSaleVolume());
            }

            // 存在品牌销冠
            if (null != brandTopSale) {
                top.setBrandCode(brandTopSale.getBrandCode());
                top.setBrandName(brandTopSale.getBrandName());
                top.setBrandSaleAmount(brandTopSale.getBrandSaleAmount());
                top.setBrandSaleVolume(brandTopSale.getBrandSaleVolume());
            }
            return top;
        }
        return null;
    }

    /**
     * 查询门店品类，品牌月度销冠数据 功能描述: <br>
     * 本月不走缓存,其他月可走缓存 〈功能详细描述〉
     *
     * @param storeCode
     * @param month
     * @param isCache
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    private BiStoreSaleTopDto queryStoreLastYearSaleTopMonth(String storeCode, String month) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("storeCode", storeCode);
        // 当前月
        paramMap.put("month", DateUtils.getThisMonth());
        // 品类销冠
        BiStoreSaleTopDto cateTopSale = dalClient.queryForObject(
                "BI_STORE_DATA_WAP.QUERY_STORE_LAST_YEAR_CATE_SALE_TOP_MONTH", paramMap, BiStoreSaleTopDto.class);
        BiStoreSaleTopDto brandTopSale;
        String key = String.format(STORE_TOP_SALE_PREFIX, storeCode, month);
        String saleTop = redisClient.get(key);
        // 缓存存在时
        if (StringUtils.isNotBlank(saleTop)) {
            // 转换成对象
            brandTopSale = GsonUtils.fromJson(saleTop, new TypeToken<BiStoreSaleTopDto>() {
            });
        } else {
            // 去年同月
            paramMap.put("month", month);
            // 品牌销冠
            brandTopSale = dalClient.queryForObject("BI_STORE_DATA_WAP.QUERY_STORE_LAST_YEAR_BRAND_SALE_TOP_MONTH",
                    paramMap, BiStoreSaleTopDto.class);
            if (null != brandTopSale) {
                // 缓存两个小时
                redisClient.setex(key, CacheKeyConstants.TWO_HOURS_EXPIRE_TIME, GsonUtils.toJson(brandTopSale, false));
            }
        }

        // 存在数据时
        if (null != cateTopSale || null != brandTopSale) {
            BiStoreSaleTopDto top = new BiStoreSaleTopDto();
            // 存在品类销冠
            if (null != cateTopSale) {
                top.setCateCode(cateTopSale.getCateCode());
                top.setCateSaleAmount(cateTopSale.getCateSaleAmount());
                top.setCateSaleVolume(cateTopSale.getCateSaleVolume());
            }

            // 存在品牌销冠
            if (null != brandTopSale) {
                top.setBrandCode(brandTopSale.getBrandCode());
                top.setBrandName(brandTopSale.getBrandName());
                top.setBrandSaleAmount(brandTopSale.getBrandSaleAmount());
                top.setBrandSaleVolume(brandTopSale.getBrandSaleVolume());
            }
            return top;
        }
        return null;
    }

    /**
     * 查询门店日销售详情 功能描述: <br>
     * 〈功能详细描述〉
     *
     * @param storeCode
     * @param month
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @Override
    public List<BiStoreSaleDetailDto> queryStoreSaleDetailByDay(String storeCode, String month) {
        LOGGER.info("enter queryStoreSaleDetailByDay, storeCode={},month={}", storeCode, month);
        // 详情数组
        List<BiStoreSaleDetailDto> detailList = new ArrayList<BiStoreSaleDetailDto>();

        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("storeCode", storeCode);
        List<String> days = getDaysOnMonth(month);
        // 设置有效天数
        paramMap.put("days", days);
        // 查询
        List<BiStoreSaleDayDto> saleDays = dalClient.queryForList("BI_STORE_DATA_WAP.QUERY_STORE_AMOUNT_DAYS",
                paramMap, BiStoreSaleDayDto.class);
        LOGGER.debug("queryStoreSaleDetailByDay saleDays={}", saleDays);
        Map<String, BiStoreSaleDayDto> saleMap;
        // 将销售数据转换成MAP格式
        if (CollectionUtils.isNotEmpty(saleDays)) {
            saleMap = Maps.uniqueIndex(saleDays, new Function<BiStoreSaleDayDto, String>() {
                @Override
                public String apply(BiStoreSaleDayDto input) {
                    return input.getSaleDate();
                }
            });
        } else {
            saleMap = new HashMap<String, BiStoreSaleDayDto>();
        }
        BiStoreSaleDayDto saleDay;
        for (String day : days) {
            if (DateUtils.getDateStrByFormat(null, "yyyyMMdd").equals(day)) {
                // 添加当日实时数据
                detailList.add(getCurrDayDetail(storeCode));
            } else {
                saleDay = saleMap.get(day);
                if (null == saleDay) {
                    saleDay = new BiStoreSaleDayDto();
                    saleDay.setSaleDate(day);
                }
                detailList.add(convertSaleDayDetail(saleDay));
            }
        }
        return detailList;
    }

    /**
     * 转换门店日销售数据对象 功能描述: <br>
     * 〈功能详细描述〉
     *
     * @param detail
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    private BiStoreSaleDetailDto convertSaleDayDetail(BiStoreSaleDayDto detail) {
        BiStoreSaleDetailDto dayDetail = new BiStoreSaleDetailDto();
        // 格式化时间
        Date saleDate = DateUtils.getDateByStr(detail.getSaleDate(), "yyyyMMdd");
        // 格式化销售日期
        dayDetail.setSaleDate(DateUtils.getDateStrByFormat(saleDate, "yyyy-MM-dd"));
        // 处理同比日期
        dayDetail.setRateDate(DateUtils.getYoyDate(saleDate, "yyyy年MM月dd日"));
        BigDecimal saleAmount;
        // 总销售额
        if (StringUtils.isNotBlank(detail.getSaleAmount())) {
            saleAmount = DJStringUtils.bigDecimal(detail.getSaleAmount());
        } else {
            saleAmount = BigDecimal.ZERO;
        }
        dayDetail.setSaleAmount(BiSaleAmountDto.valueOf(saleAmount));
        // 增加展示万元单位销售额
        dayDetail.setSaleAmountShow(BiSaleAmountDto.valueOf(saleAmount, BiSaleAmountDto.UNIT_WAN));

        // 正向销售额
        if (StringUtils.isNotBlank(detail.getPositiveSaleAmount())) {
            dayDetail.setPositiveSaleAmount(BiSaleAmountDto.valueOf(DJStringUtils.bigDecimal(detail
                    .getPositiveSaleAmount())));
        } else {
            dayDetail.setPositiveSaleAmount(BiSaleAmountDto.zero());
        }
        // 正向销售数量
        if (StringUtils.isNotBlank(detail.getPositiveSaleVolume())) {
            dayDetail.setPositiveSaleVolume(detail.getPositiveSaleVolume());
        } else {
            dayDetail.setPositiveSaleVolume("0");
        }
        // 逆向销售额
        if (StringUtils.isNotBlank(detail.getNegativeSaleAmount())) {
            dayDetail.setNegativeSaleAmount(BiSaleAmountDto.valueOf(DJStringUtils.bigDecimal(detail
                    .getNegativeSaleAmount())));
        } else {
            dayDetail.setNegativeSaleAmount(BiSaleAmountDto.zero());
        }
        // 逆向销售数量
        if (StringUtils.isNotBlank(detail.getNegativeSaleVolume())) {
            dayDetail.setNegativeSaleVolume(detail.getNegativeSaleVolume());
        } else {
            dayDetail.setNegativeSaleVolume("0");
        }
        // 进销差销售额
        if (StringUtils.isNotBlank(detail.getJxcAmount())) {
            dayDetail.setJxcAmount(BiSaleAmountDto.valueOf(DJStringUtils.bigDecimal(detail.getJxcAmount())));
        } else {
            dayDetail.setJxcAmount(BiSaleAmountDto.zero());
        }
        // 去年同期数据存在时
        if (StringUtils.isNotBlank(detail.getLastYearSaleAmount())) {
            // 同比
            dayDetail.setSaleRate(DJStringUtils.calculateYoyString(saleAmount,
                    DJStringUtils.bigDecimal(detail.getLastYearSaleAmount())));
        } else {
            dayDetail.setSaleRate("0.0");
        }

        return dayDetail;
    }

    /**
     * 获取当前日期的实时销售数据 功能描述: <br>
     * 〈功能详细描述〉
     *
     * @param storeCode
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    private BiStoreSaleDetailDto getCurrDayDetail(String storeCode) {
        BiStoreSaleDetailDto dayDetail = new BiStoreSaleDetailDto();
        // 同比日期
        dayDetail.setRateDate(DateUtils.getYoyDate(new Date(), "yyyy年MM月dd日"));
        // 日期
        dayDetail.setSaleDate(DateUtils.getDateStrByFormat(null, "yyyy-MM-dd"));
        // 取门店实时数据
        Map<String, Object> storeSaleRedis = biKafkaRealTimeSaleService.getStoreSaleDataFromRedis(storeCode);
        // 门店去年同期销售金额
        BigDecimal lasYearSaleAmount = biKafkaRealTimeSaleService.queryLastYearStoreSaleAmount(storeCode);
        // 实时数据存在时
        if (null != storeSaleRedis) {
            BigDecimal saleAmount;
            // 总销售额
            if (null != storeSaleRedis.get("payAmt")) {
                saleAmount = DJStringUtils.bigDecimal(String.valueOf(storeSaleRedis.get("payAmt")));
            } else {
                saleAmount = BigDecimal.ZERO;
            }
            dayDetail.setSaleAmount(BiSaleAmountDto.valueOf(saleAmount));
            // 增加展示万元单位销售额
            dayDetail.setSaleAmountShow(BiSaleAmountDto.valueOf(saleAmount, BiSaleAmountDto.UNIT_WAN));
            // 正向销售额
            if (null != storeSaleRedis.get("payAmt_f")) {
                dayDetail
                        .setPositiveSaleAmount(BiSaleAmountDto.valueOf(String.valueOf(storeSaleRedis.get("payAmt_f"))));
            } else {
                dayDetail.setPositiveSaleAmount(BiSaleAmountDto.zero());
            }
            // 正向销售数量
            dayDetail.setPositiveSaleVolume(DJStringUtils.formatVolume(storeSaleRedis.get("saleCnt_f")));

            // 逆向销售额
            if (null != storeSaleRedis.get("payAmt_r")) {
                dayDetail
                        .setNegativeSaleAmount(BiSaleAmountDto.valueOf(String.valueOf(storeSaleRedis.get("payAmt_r"))));
            } else {
                dayDetail.setNegativeSaleAmount(BiSaleAmountDto.zero());
            }
            // 逆向销售数量
            dayDetail.setNegativeSaleVolume(DJStringUtils.formatVolume(storeSaleRedis.get("saleCnt_r")));

            //进销差销售额
            if (null != storeSaleRedis.get("jxc_amount")) {
                dayDetail.setJxcAmount(BiSaleAmountDto.valueOf(String.valueOf(storeSaleRedis.get("jxc_amount"))));
            } else {
                dayDetail.setJxcAmount(BiSaleAmountDto.zero());
            }
            
            // 去年同期数据存在时
            if (null != lasYearSaleAmount) {
                // 同比
                dayDetail.setSaleRate(DJStringUtils.calculateYoyString(saleAmount, lasYearSaleAmount));
            } else {
                dayDetail.setSaleRate("0.0");
            }
        } else {
            // 默认销售额，销售数量
            dayDetail.setSaleAmount(BiSaleAmountDto.zero());
            // 增加展示万元单位销售额
            dayDetail.setSaleAmountShow(BiSaleAmountDto.zero());
            dayDetail.setPositiveSaleAmount(BiSaleAmountDto.zero());
            dayDetail.setPositiveSaleVolume("0");
            dayDetail.setNegativeSaleAmount(BiSaleAmountDto.zero());
            dayDetail.setNegativeSaleVolume("0");
            dayDetail.setJxcAmount(BiSaleAmountDto.zero());
            // 同比 - 去年同期数据存在时
            if (null != lasYearSaleAmount) {
                dayDetail.setSaleRate(DJStringUtils.calculateYoyString(BigDecimal.ZERO, lasYearSaleAmount));
            } else {
                dayDetail.setSaleRate("0.0");
            }
        }
        return dayDetail;
    }

    /**
     * 查询品类实时数据 功能描述: <br>
     * 〈功能详细描述〉
     *
     * @param storeCode
     * @param cateCode
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    private BiStoreSaleDetailDto getCurrDayCateSaleDetail(String storeCode, String cateCode) {
        BiStoreSaleDetailDto dayDetail = new BiStoreSaleDetailDto();
        // 格式化时间
        Date saleDate = new Date();
        // 格式化销售日期
        dayDetail.setSaleDate(DateUtils.getDateStrByFormat(saleDate, "yyyy-MM-dd"));
        // 处理同比日期
        dayDetail.setRateDate(DateUtils.getYoyDate(saleDate, "yyyy年MM月dd日"));

        // 取门店实时数据
        Map<String, Object> storeSaleRedis = biKafkaRealTimeSaleService.getStoreCateSaleDataFromRedis(storeCode,
                cateCode);
        // 门店去年同期销售金额
        BigDecimal lasYearSaleAmount = biKafkaRealTimeSaleService.queryLastYearStoreCateSaleAmount(storeCode, cateCode);
        // 实时数据存在时
        if (null != storeSaleRedis) {
            BigDecimal saleAmount;
            // 总销售额
            if (null != storeSaleRedis.get("payAmt")) {
                saleAmount = DJStringUtils.bigDecimal(String.valueOf(storeSaleRedis.get("payAmt")));
            } else {
                saleAmount = BigDecimal.ZERO;
            }
            dayDetail.setSaleAmount(BiSaleAmountDto.valueOf(saleAmount));

            // 增加展示万元单位销售额
            dayDetail.setSaleAmountShow(BiSaleAmountDto.valueOf(saleAmount, BiSaleAmountDto.UNIT_WAN));

            // 正向销售额
            if (null != storeSaleRedis.get("payAmt_f")) {
                dayDetail
                        .setPositiveSaleAmount(BiSaleAmountDto.valueOf(String.valueOf(storeSaleRedis.get("payAmt_f"))));
            } else {
                dayDetail.setPositiveSaleAmount(BiSaleAmountDto.zero());
            }
            // 正向销售数量
            dayDetail.setPositiveSaleVolume(DJStringUtils.formatVolume(storeSaleRedis.get("saleCnt_f")));

            // 逆向销售额
            if (null != storeSaleRedis.get("payAmt_r")) {
                dayDetail
                        .setNegativeSaleAmount(BiSaleAmountDto.valueOf(String.valueOf(storeSaleRedis.get("payAmt_r"))));
            } else {
                dayDetail.setNegativeSaleAmount(BiSaleAmountDto.zero());
            }
            // 逆向销售数量
            dayDetail.setNegativeSaleVolume(DJStringUtils.formatVolume(storeSaleRedis.get("saleCnt_r")));

            // 进销差销售额
            if (null != storeSaleRedis.get("jxc_amount")) {
                dayDetail.setJxcAmount(BiSaleAmountDto.valueOf(String.valueOf(storeSaleRedis.get("jxc_amount"))));
            } else {
                dayDetail.setJxcAmount(BiSaleAmountDto.zero());
            }
            
            // 去年同期数据存在时
            if (null != lasYearSaleAmount) {
                // 同比
                dayDetail.setSaleRate(DJStringUtils.calculateYoyString(saleAmount, lasYearSaleAmount));
            } else {
                dayDetail.setSaleRate("0.0");
            }
        } else {
            // 默认销售额，销售数量
            dayDetail.setSaleAmount(BiSaleAmountDto.zero());
            // 增加展示万元单位销售额
            dayDetail.setSaleAmountShow(BiSaleAmountDto.zero());
            dayDetail.setPositiveSaleAmount(BiSaleAmountDto.zero());
            dayDetail.setPositiveSaleVolume("0");
            dayDetail.setNegativeSaleAmount(BiSaleAmountDto.zero());
            dayDetail.setNegativeSaleVolume("0");
            dayDetail.setJxcAmount(BiSaleAmountDto.zero());
            // 去年同期数据存在时
            if (null != lasYearSaleAmount) {
                // 同比
                dayDetail.setSaleRate(DJStringUtils.calculateYoyString(BigDecimal.ZERO, lasYearSaleAmount));
            } else {
                dayDetail.setSaleRate("0.0");
            }
        }
        return dayDetail;
    }

    /**
     * 根据月份查询有效天 功能描述: <br>
     * 〈功能详细描述〉
     *
     * @param month
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    private List<String> getDaysOnMonth(String month) {
        Date time = DateUtils.getDateByStr(month, "yyyy-MM");
        // 判断是否是本月
        if (DateUtils.getThisMonth().equals(DateUtils.getDateStrByFormat(time, "yyyyMM"))) {
            Date day = DateUtils.getFirstDateOfMonth(time);
            List<String> days = new ArrayList<String>();
            String today = DateUtils.getDateStrByFormat(null, "yyyyMMdd");
            // 日期不等于当日时
            while (!today.equals(DateUtils.getDateStrByFormat(day, "yyyyMMdd"))) {
                days.add(DateUtils.getDateStrByFormat(day, "yyyyMMdd"));
                day = DateUtils.getNextDay(day, 1);
            }
            // 添加今天
            days.add(today);
            return days;
        } else {
            Date day = DateUtils.getFirstDateOfMonth(time);
            Date endDay = DateUtils.getLastDateOfMonth(time);
            List<String> days = new ArrayList<String>();
            // 日期不等于当日时
            while (!DateUtils.getDateStrByFormat(endDay, "yyyyMMdd").equals(
                    DateUtils.getDateStrByFormat(day, "yyyyMMdd"))) {
                days.add(DateUtils.getDateStrByFormat(day, "yyyyMMdd"));
                day = DateUtils.getNextDay(day, 1);
            }
            // 补充最后一天
            days.add(DateUtils.getDateStrByFormat(day, "yyyyMMdd"));
            return days;
        }
    }

    /**
     * 查询门店月销售详情 功能描述: <br>
     * 〈功能详细描述〉
     *
     * @param storeCode
     * @param year
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @Override
    public List<BiStoreSaleDetailDto> queryStoreSaleDetailByMonth(String storeCode, String year) {
        LOGGER.info("enter queryStoreSaleDetailByMonth, storeCode={},year={}", storeCode, year);
        // 详情数组
        List<BiStoreSaleDetailDto> detailList = new ArrayList<BiStoreSaleDetailDto>();
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("storeCode", storeCode);
        List<String> months = getMonthsOnYear(year);
        // 设置有效天数
        paramMap.put("months", months);
        // 查询
        List<BiStoreSaleDayDto> saleMonths = dalClient.queryForList("BI_STORE_DATA_WAP.QUERY_STORE_AMOUNT_MONTHS",
                paramMap, BiStoreSaleDayDto.class);
        LOGGER.debug("queryStoreSaleDetailByMonth saleMonths={}", saleMonths);
        Map<String, BiStoreSaleDayDto> saleMap;
        // 将销售数据转换成MAP格式
        if (CollectionUtils.isNotEmpty(saleMonths)) {
            saleMap = Maps.uniqueIndex(saleMonths, new Function<BiStoreSaleDayDto, String>() {
                @Override
                public String apply(BiStoreSaleDayDto input) {
                    return input.getSaleDate();
                }
            });
        } else {
            saleMap = new HashMap<String, BiStoreSaleDayDto>();
        }
        BiStoreSaleDayDto saleMonth;
        for (String month : months) {
            saleMonth = saleMap.get(month);
            if (null == saleMonth) {
                // 创建默认对象,设置销售日期
                saleMonth = new BiStoreSaleDayDto();
                saleMonth.setSaleDate(month);
            }
            detailList.add(convertSaleMonthsDetail(saleMonth, storeCode));
        }
        return detailList;
    }

    /**
     * 转换门店月份销售数据详情 功能描述: <br>
     * 〈功能详细描述〉
     *
     * @param detail
     * @param storeCode
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    private BiStoreSaleDetailDto convertSaleMonthsDetail(BiStoreSaleDayDto detail, String storeCode) {
        BiStoreSaleDetailDto dayDetail = new BiStoreSaleDetailDto();
        // 格式化时间
        Date saleDate = DateUtils.getDateByStr(detail.getSaleDate(), "yyyyMM");
        // 格式化销售日期
        dayDetail.setSaleDate(DateUtils.getDateStrByFormat(saleDate, "yyyy-MM"));
        // 处理同比日期
        dayDetail.setRateDate(DateUtils.getDateStrByFormat(DateUtils.getNextYear(saleDate, -1), "yyyy年MM月"));
        BigDecimal saleAmount;
        // 总销售额
        if (StringUtils.isNotBlank(detail.getSaleAmount())) {
            saleAmount = DJStringUtils.bigDecimal(detail.getSaleAmount());
        } else {
            saleAmount = BigDecimal.ZERO;
        }
        BigDecimal positiveSaleAmount;
        // 正向销售额
        if (StringUtils.isNotBlank(detail.getPositiveSaleAmount())) {
            positiveSaleAmount = DJStringUtils.bigDecimal(detail.getPositiveSaleAmount());
        } else {
            positiveSaleAmount = BigDecimal.ZERO;
        }
        BigDecimal positiveSaleVolume;
        // 正向销售数量
        if (StringUtils.isNotBlank(detail.getPositiveSaleVolume())) {
            positiveSaleVolume = DJStringUtils.bigDecimal(detail.getPositiveSaleVolume());
        } else {
            positiveSaleVolume = BigDecimal.ZERO;
        }
        BigDecimal negativeSaleAmount;
        // 逆向销售额
        if (StringUtils.isNotBlank(detail.getNegativeSaleAmount())) {
            negativeSaleAmount = DJStringUtils.bigDecimal(detail.getNegativeSaleAmount());
        } else {
            negativeSaleAmount = BigDecimal.ZERO;
        }
        BigDecimal negativeSaleVolume;
        // 逆向销售数量
        if (StringUtils.isNotBlank(detail.getNegativeSaleVolume())) {
            negativeSaleVolume = DJStringUtils.bigDecimal(detail.getNegativeSaleVolume());
        } else {
            negativeSaleVolume = BigDecimal.ZERO;
        }
        
        BigDecimal jxcSaleAmount;
        // 进销差销售额
        if (StringUtils.isNotBlank(detail.getJxcAmount())) {
            jxcSaleAmount = DJStringUtils.bigDecimal(detail.getJxcAmount());
        } else {
            jxcSaleAmount = BigDecimal.ZERO;
        }
        BigDecimal lasYearSaleAmount;
        // 去年同期数据存在时
        if (StringUtils.isNotBlank(detail.getLastYearSaleAmount())) {
            lasYearSaleAmount = DJStringUtils.bigDecimal(detail.getLastYearSaleAmount());
        } else {
            lasYearSaleAmount = BigDecimal.ZERO;
        }

        // 本月销售数据 ,需要加当日实时销售销售
        if (DateUtils.getThisMonth().equals(detail.getSaleDate())) {
            // 取门店实时数据
            Map<String, Object> storeSaleRedis = biKafkaRealTimeSaleService.getStoreSaleDataFromRedis(storeCode);
            // 实时数据存在时
            if (null != storeSaleRedis) {
                // 总销售额
                if (null != storeSaleRedis.get("payAmt")) {
                    saleAmount = saleAmount.add(DJStringUtils.bigDecimal(String.valueOf(storeSaleRedis.get("payAmt"))));
                }
                // 正向销售额
                if (null != storeSaleRedis.get("payAmt_f")) {
                    positiveSaleAmount = positiveSaleAmount.add(DJStringUtils.bigDecimal(String.valueOf(storeSaleRedis
                            .get("payAmt_f"))));
                }
                // 正向销售数量
                if (null != storeSaleRedis.get("saleCnt_f")) {
                    positiveSaleVolume = positiveSaleVolume.add(DJStringUtils.bigDecimal(String.valueOf(storeSaleRedis
                            .get("saleCnt_f"))));
                }
                // 逆向销售额
                if (null != storeSaleRedis.get("payAmt_r")) {
                    negativeSaleAmount = negativeSaleAmount.add(DJStringUtils.bigDecimal(String.valueOf(storeSaleRedis
                            .get("payAmt_r"))));
                }
                // 逆向销售数量
                if (null != storeSaleRedis.get("saleCnt_r")) {
                    negativeSaleVolume = negativeSaleVolume.add(DJStringUtils.bigDecimal(String.valueOf(storeSaleRedis
                            .get("saleCnt_r"))));
                }
            }
            // 月初
            if ("01".equals(DateUtils.getDateStrByFormat(new Date(), "dd"))) {
                dayDetail.setSystemTime("暂未统计");
            } else {
                // 昨日时间
                dayDetail.setSystemTime("截至" + DateUtils.getDateStrByFormat(DateUtils.getNextDay(new Date(), -1), "MM-dd"));
            }
        }else{
            dayDetail.setSystemTime("截至" + DateUtils.getDateStrByFormat( DateUtils.getLastDateOfMonth(saleDate), "MM-dd"));
        }
        // 设置销售额, 正向, 逆向销售额销量
        dayDetail.setSaleAmount(BiSaleAmountDto.valueOf(saleAmount));
        // 增加展示万元单位销售额
        dayDetail.setSaleAmountShow(BiSaleAmountDto.valueOf(saleAmount, BiSaleAmountDto.UNIT_WAN));
        dayDetail.setPositiveSaleAmount(BiSaleAmountDto.valueOf(positiveSaleAmount));
        dayDetail.setPositiveSaleVolume(String.valueOf(positiveSaleVolume.longValue()));
        dayDetail.setNegativeSaleAmount(BiSaleAmountDto.valueOf(negativeSaleAmount));
        dayDetail.setNegativeSaleVolume(String.valueOf(negativeSaleVolume.longValue()));
        dayDetail.setJxcAmount(BiSaleAmountDto.valueOf(jxcSaleAmount));
        
        // 同比
        dayDetail.setSaleRate(DJStringUtils.calculateYoyString(saleAmount, lasYearSaleAmount));
        return dayDetail;
    }

    /**
     * 根据年查询有效月份 功能描述: <br>
     * 〈功能详细描述〉
     *
     * @param year
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    private List<String> getMonthsOnYear(String year) {
        Date time = DateUtils.getDateByStr(year, "yyyy");
        // 是否是今年
        if (DateUtils.getDateStrByFormat(null, "yyyy").equals(DateUtils.getDateStrByFormat(time, "yyyy"))) {
            Date thisMonth = new Date();
            List<String> months = new ArrayList<String>();
            // 月份小于当前月时间
            while (thisMonth.getTime() > time.getTime()) {
                months.add(DateUtils.getDateStrByFormat(time, "yyyyMM"));
                time = DateUtils.getNextMonth(time);
            }
            // 为空时,添加本月数据
            if (CollectionUtils.isEmpty(months)) {
                months.add(DateUtils.getDateStrByFormat(null, "yyyyMM"));
            }
            return months;
        } else {
            Date nextYear = DateUtils.getNextYear(time);
            List<String> months = new ArrayList<String>();
            // 月份小月下一年年初
            while (time.getTime() < nextYear.getTime()) {
                months.add(DateUtils.getDateStrByFormat(time, "yyyyMM"));
                time = DateUtils.getNextMonth(time);
            }
            return months;
        }
    }

    /**
     * 品类日维度销售排行 功能描述: <br>
     * 〈功能详细描述〉
     *
     * @param storeCode
     * @param day
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @Override
    public List<BiStoreCateSaleDetailDto> queryStoreCateSaleAmountDay(String storeCode, String day) {
        LOGGER.info("enter queryStoreCateSaleAmountDay, storeCode={},day={}", storeCode, day);

        // 判断是否是查询今天的销售排行
        if (DateUtils.getDateStrByFormat(null, "yyyy-MM-dd").equals(day)) {
            // 查询门店总销售额
            BigDecimal totalAmount;
            Map<String, Object> todaySale = biKafkaRealTimeSaleService.getStoreSaleDataFromRedis(storeCode);
            if (null == todaySale || null == todaySale.get("payAmt")) {
                totalAmount = BigDecimal.ZERO;
            } else {
                totalAmount = DJStringUtils.bigDecimal(String.valueOf(todaySale.get("payAmt")));
            }

            // 查询门店品牌实时销售数据
            List<Map<String, Object>> cateSales = biKafkaRealTimeSaleService.getStoreCateSaleDataFromRedis(storeCode);
            // 数据不为空时
            if (CollectionUtils.isEmpty(cateSales)) {
                return Collections.emptyList();
            }
            List<BiStoreCateSaleDetailDto> cateSaleList = new ArrayList<BiStoreCateSaleDetailDto>();
            // 根据销售额排序
            Collections.sort(cateSales, new Comparator<Map<String, Object>>() {
                @Override
                public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                    if (null != o2.get("payAmt") && null != o1.get("payAmt")) {
                        return DJStringUtils.bigDecimal(String.valueOf(o2.get("payAmt"))).compareTo(
                                DJStringUtils.bigDecimal(String.valueOf(o1.get("payAmt"))));
                    } else if (null == o2.get("payAmt")) {
                        return -1;
                    } else if (null == o1.get("payAmt")) {
                        return 1;
                    }
                    // 降序
                    return 0;
                }
            });
            BiStoreCateSaleDetailDto cateDetail;
            BigDecimal saleAmount;
            BigDecimal saleRate;
            BigDecimal positiveSaleAmount;
            BigDecimal negativeSaleAmount;
            for (Map<String, Object> sale : cateSales) {
                // 获取品类编码
                String cateCode = String.valueOf(sale.get("deptCd"));
                String cateName = ConfigConstants.DEFAULT_CATE_MAP.get(cateCode);
                // 判断品类编码和品类名称是否为空
                if (StringUtils.isBlank(cateCode) || StringUtils.isBlank(cateName)) {
                    // 如果品类编码,或者品类名称不存在,则不处理该品类信息
                    continue;
                }
                cateDetail = new BiStoreCateSaleDetailDto();
                cateDetail.setCateCode(cateCode);
                cateDetail.setCateName(cateName);
                if (null != sale.get("payAmt")) {
                    saleAmount = DJStringUtils.bigDecimal(String.valueOf(sale.get("payAmt")));
                } else {
                    saleAmount = BigDecimal.ZERO;
                }
                // 销售额
                cateDetail.setSaleAmount(BiSaleAmountDto.valueOf(saleAmount));

                // 计算同期销售比例
                saleRate = biKafkaRealTimeSaleService.queryStoreCateDaySaleYoy(storeCode, cateCode);
                if (null != saleRate) {
                    cateDetail.setSaleRate(saleRate.multiply(new BigDecimal(100))
                            .setScale(BigDecimal.ONE.intValue(), BigDecimal.ROUND_DOWN).toString());
                } else {
                    cateDetail.setSaleRate("0.0");
                }

                // 销量
                cateDetail.setSaleVolume(DJStringUtils.formatVolume(sale.get("saleCnt")));

                // 计算品类占比
                cateDetail.setSaleTotalRate(DJStringUtils.calculateRateString(saleAmount.toString(),
                        totalAmount.toString()));

                // 正向销售额,销量
                if (null != sale.get("payAmt_f")) {
                    positiveSaleAmount = DJStringUtils.bigDecimal(String.valueOf(sale.get("payAmt_f")));
                } else {
                    positiveSaleAmount = BigDecimal.ZERO;
                }
                cateDetail.setPositiveSaleAmount(BiSaleAmountDto.valueOf(positiveSaleAmount));
                cateDetail.setPositiveSaleVolume(DJStringUtils.formatVolume(sale.get("saleCnt_f")));

                // 逆向销售额,销量
                if (null != sale.get("payAmt_r")) {
                    negativeSaleAmount = DJStringUtils.bigDecimal(String.valueOf(sale.get("payAmt_r")));
                } else {
                    negativeSaleAmount = BigDecimal.ZERO;
                }
                cateDetail.setNegativeSaleAmount(BiSaleAmountDto.valueOf(negativeSaleAmount));
                cateDetail.setNegativeSaleVolume(DJStringUtils.formatVolume(sale.get("saleCnt_r")));

                cateSaleList.add(cateDetail);
            }
            return cateSaleList;
        } else {
            // 查询门店总销售额
            BigDecimal totalAmount;
            Date time = DateUtils.getDateByStr(day, "yyyy-MM-dd");
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("storeCode", storeCode);
            // 设置有效天数
            paramMap.put("day", DateUtils.getDateStrByFormat(time, "yyyyMMdd"));

            // 查询日总销售数据
            BiStoreSaleDayDto saleTotal = dalClient.queryForObject("BI_STORE_DATA_WAP.QUERY_STORE_AMOUNT_BY_DAY",
                    paramMap, BiStoreSaleDayDto.class);
            if (null == saleTotal || StringUtils.isBlank(saleTotal.getSaleAmount())) {
                totalAmount = BigDecimal.ZERO;
            } else {
                totalAmount = DJStringUtils.bigDecimal(saleTotal.getSaleAmount());
            }

            // 查询
            List<BiStoreCateSaleDayDto> saleDays = dalClient.queryForList(
                    "BI_STORE_DATA_WAP.QUERY_STORE_CATE_AMOUNT_RANK_DAY", paramMap, BiStoreCateSaleDayDto.class);
            LOGGER.debug("queryStoreCateSaleAmountDay saleDays={}", saleDays);
            // 数据为空时
            if (CollectionUtils.isEmpty(saleDays)) {
                return Collections.emptyList();
            }
            BiStoreCateSaleDetailDto cateDetail;
            BigDecimal saleAmount;
            BigDecimal lastYearAmount;
            BigDecimal positiveSaleAmount;
            BigDecimal negativeSaleAmount;
            List<BiStoreCateSaleDetailDto> cateSaleList = new ArrayList<BiStoreCateSaleDetailDto>();
            for (BiStoreCateSaleDayDto saleDay : saleDays) {
                // 获取品类编码
                String cateCode = saleDay.getCateCode();
                String cateName = ConfigConstants.DEFAULT_CATE_MAP.get(cateCode);
                // 判断品类编码和品类名称是否为空
                if (StringUtils.isBlank(cateCode) || StringUtils.isBlank(cateName)) {
                    // 如果品类编码,或者品类名称不存在,则不处理该品类信息
                    continue;
                }
                cateDetail = new BiStoreCateSaleDetailDto();
                cateDetail.setCateCode(cateCode);
                cateDetail.setCateName(cateName);
                if (StringUtils.isNotBlank(saleDay.getSaleAmount())) {
                    saleAmount = DJStringUtils.bigDecimal(saleDay.getSaleAmount());
                } else {
                    saleAmount = BigDecimal.ZERO;
                }
                // 销售额
                cateDetail.setSaleAmount(BiSaleAmountDto.valueOf(saleAmount));

                // 去年同期销售额
                if (StringUtils.isNotBlank(saleDay.getLastYearSaleAmount())) {
                    lastYearAmount = DJStringUtils.bigDecimal(saleDay.getLastYearSaleAmount());
                } else {
                    lastYearAmount = BigDecimal.ZERO;
                }
                // 计算同期销售比例
                cateDetail.setSaleRate(DJStringUtils.calculateYoyString(saleAmount, lastYearAmount));
                // 销量
                cateDetail.setSaleVolume(StringUtils.isBlank(saleDay.getSaleVolume()) ? "0" : saleDay.getSaleVolume());

                // 计算品类占比
                cateDetail.setSaleTotalRate(DJStringUtils.calculateRateString(saleAmount.toString(),
                        totalAmount.toString()));

                // 正向销售额,销量
                if (StringUtils.isNotBlank(saleDay.getPositiveSaleAmount())) {
                    positiveSaleAmount = DJStringUtils.bigDecimal(saleDay.getPositiveSaleAmount());
                } else {
                    positiveSaleAmount = BigDecimal.ZERO;
                }
                cateDetail.setPositiveSaleAmount(BiSaleAmountDto.valueOf(positiveSaleAmount));

                // 逆向销售额,销量
                if (StringUtils.isNotBlank(saleDay.getNegativeSaleAmount())) {
                    negativeSaleAmount = DJStringUtils.bigDecimal(saleDay.getNegativeSaleAmount());
                } else {
                    negativeSaleAmount = BigDecimal.ZERO;
                }
                cateDetail.setNegativeSaleAmount(BiSaleAmountDto.valueOf(negativeSaleAmount));
                cateSaleList.add(cateDetail);
            }
            return cateSaleList;
        }
    }

    /**
     * 品类日维度销量排行 功能描述: <br>
     * 〈功能详细描述〉
     *
     * @param storeCode
     * @param day
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @Override
    public List<BiStoreCateSaleDetailDto> queryStoreCateSaleVolumeDay(String storeCode, String day) {
        LOGGER.info("enter queryStoreCateSaleVolumeDay, storeCode={},day={}", storeCode, day);

        // 判断是否是查询今天的销售排行
        if (DateUtils.getDateStrByFormat(null, "yyyy-MM-dd").equals(day)) {
            // 实时总销量
            BigDecimal totalVolume;
            Map<String, Object> todaySale = biKafkaRealTimeSaleService.getStoreSaleDataFromRedis(storeCode);
            if (null == todaySale || null == todaySale.get("saleCnt")) {
                totalVolume = BigDecimal.ZERO;
            } else {
                totalVolume = DJStringUtils.bigDecimal(String.valueOf(todaySale.get("saleCnt")));
            }
            // 查询门店品牌实时销售数据
            List<Map<String, Object>> cateSales = biKafkaRealTimeSaleService.getStoreCateSaleDataFromRedis(storeCode);
            // 数据不为空时
            if (CollectionUtils.isEmpty(cateSales)) {
                return Collections.emptyList();
            }
            List<BiStoreCateSaleDetailDto> cateSaleList = new ArrayList<BiStoreCateSaleDetailDto>();
            // 根据销售额排序
            Collections.sort(cateSales, new Comparator<Map<String, Object>>() {
                @Override
                public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                    if (null != o2.get("saleCnt") && null != o1.get("saleCnt")) {
                        return DJStringUtils.bigDecimal(String.valueOf(o2.get("saleCnt"))).compareTo(
                                DJStringUtils.bigDecimal(String.valueOf(o1.get("saleCnt"))));
                    } else if (null == o2.get("saleCnt")) {
                        return -1;
                    } else if (null == o1.get("saleCnt")) {
                        return 1;
                    }
                    // 降序
                    return 0;
                }
            });
            BiStoreCateSaleDetailDto cateDetail;
            BigDecimal saleAmount;
            String saleVolume;
            BigDecimal lastYearVolume;
            for (Map<String, Object> sale : cateSales) {
                // 获取品类编码
                String cateCode = String.valueOf(sale.get("deptCd"));
                String cateName = ConfigConstants.DEFAULT_CATE_MAP.get(cateCode);
                // 判断品类编码和品类名称是否为空
                if (StringUtils.isBlank(cateCode) || StringUtils.isBlank(cateName)) {
                    // 如果品类编码,或者品类名称不存在,则不处理该品类信息
                    continue;
                }
                cateDetail = new BiStoreCateSaleDetailDto();
                cateDetail.setCateCode(cateCode);
                cateDetail.setCateName(cateName);
                if (null != sale.get("payAmt")) {
                    saleAmount = DJStringUtils.bigDecimal(String.valueOf(sale.get("payAmt")));
                } else {
                    saleAmount = BigDecimal.ZERO;
                }
                // 销售额
                cateDetail.setSaleAmount(BiSaleAmountDto.valueOf(saleAmount));
                saleVolume = DJStringUtils.formatVolume(sale.get("saleCnt"));
                // 销量
                cateDetail.setSaleVolume(saleVolume);

                // 去年同期销量
                lastYearVolume = biKafkaRealTimeSaleService.queryLastYearStoreSaleVolume(storeCode, cateCode);

                // 计算同期销售比例
                cateDetail.setSaleRate(DJStringUtils.calculateYoyString(DJStringUtils.bigDecimal(saleVolume),
                        lastYearVolume));

                // 计算占比
                cateDetail.setSaleTotalRate(DJStringUtils.calculateRateString(saleVolume, totalVolume.toString()));

                // 正向销量
                cateDetail.setPositiveSaleVolume(DJStringUtils.formatVolume(sale.get("saleCnt_f")));

                // 逆向销量
                cateDetail.setNegativeSaleVolume(DJStringUtils.formatVolume(sale.get("saleCnt_r")));
                cateSaleList.add(cateDetail);
            }
            return cateSaleList;
        } else {
            BigDecimal totalVolume;
            Date time = DateUtils.getDateByStr(day, "yyyy-MM-dd");
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("storeCode", storeCode);
            // 设置有效天数
            paramMap.put("day", DateUtils.getDateStrByFormat(time, "yyyyMMdd"));

            // 查询日总销售数据
            BiStoreSaleDayDto saleTotal = dalClient.queryForObject("BI_STORE_DATA_WAP.QUERY_STORE_AMOUNT_BY_DAY",
                    paramMap, BiStoreSaleDayDto.class);
            if (null == saleTotal || StringUtils.isBlank(saleTotal.getSaleVolume())) {
                totalVolume = BigDecimal.ZERO;
            } else {
                totalVolume = DJStringUtils.bigDecimal(saleTotal.getSaleVolume());
            }

            // 查询
            List<BiStoreCateSaleDayDto> saleDays = dalClient.queryForList(
                    "BI_STORE_DATA_WAP.QUERY_STORE_CATE_VOLUME_RANK_DAY", paramMap, BiStoreCateSaleDayDto.class);
            LOGGER.debug("queryStoreCateSaleVolumeDay saleDays={}", saleDays);
            // 数据为空时
            if (CollectionUtils.isEmpty(saleDays)) {
                return Collections.emptyList();
            }
            BiStoreCateSaleDetailDto cateDetail;
            BigDecimal saleAmount;
            String saleVolume;
            BigDecimal lastYearVolume;
            List<BiStoreCateSaleDetailDto> cateSaleList = new ArrayList<BiStoreCateSaleDetailDto>();
            for (BiStoreCateSaleDayDto saleDay : saleDays) {
                // 获取品类编码
                String cateCode = saleDay.getCateCode();
                String cateName = ConfigConstants.DEFAULT_CATE_MAP.get(cateCode);
                // 判断品类编码和品类名称是否为空
                if (StringUtils.isBlank(cateCode) || StringUtils.isBlank(cateName)) {
                    // 如果品类编码,或者品类名称不存在,则不处理该品类信息
                    continue;
                }
                cateDetail = new BiStoreCateSaleDetailDto();
                cateDetail.setCateCode(cateCode);
                cateDetail.setCateName(cateName);
                if (StringUtils.isNotBlank(saleDay.getSaleAmount())) {
                    saleAmount = DJStringUtils.bigDecimal(saleDay.getSaleAmount());
                } else {
                    saleAmount = BigDecimal.ZERO;
                }
                // 销售额
                cateDetail.setSaleAmount(BiSaleAmountDto.valueOf(saleAmount));

                // 销量
                saleVolume = StringUtils.isBlank(saleDay.getSaleVolume()) ? "0" : saleDay.getSaleVolume();
                cateDetail.setSaleVolume(saleVolume);

                // 去年同期销量
                if (StringUtils.isNotBlank(saleDay.getLastYearVolume())) {
                    lastYearVolume = DJStringUtils.bigDecimal(saleDay.getLastYearVolume());
                } else {
                    lastYearVolume = BigDecimal.ZERO;
                }
                // 计算同期销量比例
                cateDetail.setSaleRate(DJStringUtils.calculateYoyString(DJStringUtils.bigDecimal(saleVolume),
                        lastYearVolume));

                // 计算品类占比
                cateDetail.setSaleTotalRate(DJStringUtils.calculateRateString(saleVolume, totalVolume.toString()));

                // 正向销量
                cateDetail.setPositiveSaleVolume(StringUtils.isBlank(saleDay.getPositiveSaleVolume()) ? "0" : saleDay
                        .getPositiveSaleVolume());

                // 逆向销量
                cateDetail.setNegativeSaleVolume(StringUtils.isBlank(saleDay.getNegativeSaleVolume()) ? "0" : saleDay
                        .getNegativeSaleVolume());
                cateSaleList.add(cateDetail);
            }

            return cateSaleList;
        }
    }

    /**
     * 品类月销售额排行 功能描述: <br>
     * 〈功能详细描述〉
     *
     * @param storeCode
     * @param month
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @Override
    public List<BiStoreCateSaleDetailDto> queryStoreCateSaleAmountMonth(String storeCode, String month) {
        LOGGER.info("enter queryStoreCateSaleAmountMonth, storeCode={},month={}", storeCode, month);
        BigDecimal totalAmount;
        Date time = DateUtils.getDateByStr(month, "yyyy-MM");
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("storeCode", storeCode);
        // 设置有效月份
        paramMap.put("month", DateUtils.getDateStrByFormat(time, "yyyyMM"));

        // 月销售数据
        BiStoreSaleMonthDto storeMonthSale = dalClient.queryForObject("BI_STORE_DATA_WAP.QUERY_STORE_SALE_MONTH",
                paramMap, BiStoreSaleMonthDto.class);
        if (null == storeMonthSale || StringUtils.isBlank(storeMonthSale.getSaleAmount())) {
            totalAmount = BigDecimal.ZERO;
        } else {
            totalAmount = DJStringUtils.bigDecimal(storeMonthSale.getSaleAmount());
        }

        // 查询
        List<BiStoreCateSaleDayDto> saleMonths = dalClient.queryForList(
                "BI_STORE_DATA_WAP.QUERY_STORE_CATE_AMOUNT_RANK_MONTH", paramMap, BiStoreCateSaleDayDto.class);
        LOGGER.debug("queryStoreCateSaleAmountMonth saleMonths={}", saleMonths);
        // 数据为空时
        if (CollectionUtils.isEmpty(saleMonths)) {
            return Collections.emptyList();
        }
        BiStoreCateSaleDetailDto cateDetail;
        BigDecimal saleAmount;
        BigDecimal lastYearAmount;
        BigDecimal positiveSaleAmount;
        BigDecimal negativeSaleAmount;
        List<BiStoreCateSaleDetailDto> cateSaleList = new ArrayList<BiStoreCateSaleDetailDto>();
        for (BiStoreCateSaleDayDto saleMonth : saleMonths) {
            // 获取品类编码
            String cateCode = saleMonth.getCateCode();
            String cateName = ConfigConstants.DEFAULT_CATE_MAP.get(cateCode);
            // 判断品类编码和品类名称是否为空
            if (StringUtils.isBlank(cateCode) || StringUtils.isBlank(cateName)) {
                // 如果品类编码,或者品类名称不存在,则不处理该品类信息
                continue;
            }
            cateDetail = new BiStoreCateSaleDetailDto();
            cateDetail.setCateCode(cateCode);
            cateDetail.setCateName(cateName);
            if (StringUtils.isNotBlank(saleMonth.getSaleAmount())) {
                saleAmount = DJStringUtils.bigDecimal(saleMonth.getSaleAmount());
            } else {
                saleAmount = BigDecimal.ZERO;
            }
            // 销售额
            cateDetail.setSaleAmount(BiSaleAmountDto.valueOf(saleAmount));

            // 去年同期销售额
            if (StringUtils.isNotBlank(saleMonth.getLastYearSaleAmount())) {
                lastYearAmount = DJStringUtils.bigDecimal(saleMonth.getLastYearSaleAmount());
            } else {
                lastYearAmount = BigDecimal.ZERO;
            }
            // 计算同期销售比例
            cateDetail.setSaleRate(DJStringUtils.calculateYoyString(saleAmount, lastYearAmount));

            // 计算品类占比
            cateDetail
                    .setSaleTotalRate(DJStringUtils.calculateRateString(saleAmount.toString(), totalAmount.toString()));

            // 销量
            cateDetail.setSaleVolume(StringUtils.isBlank(saleMonth.getSaleVolume()) ? "0" : saleMonth.getSaleVolume());

            // 正向销售额,销量
            if (StringUtils.isNotBlank(saleMonth.getPositiveSaleAmount())) {
                positiveSaleAmount = DJStringUtils.bigDecimal(saleMonth.getPositiveSaleAmount());
            } else {
                positiveSaleAmount = BigDecimal.ZERO;
            }
            cateDetail.setPositiveSaleAmount(BiSaleAmountDto.valueOf(positiveSaleAmount));

            // 逆向销售额,销量
            if (StringUtils.isNotBlank(saleMonth.getNegativeSaleAmount())) {
                negativeSaleAmount = DJStringUtils.bigDecimal(saleMonth.getNegativeSaleAmount());
            } else {
                negativeSaleAmount = BigDecimal.ZERO;
            }
            cateDetail.setNegativeSaleAmount(BiSaleAmountDto.valueOf(negativeSaleAmount));
            cateSaleList.add(cateDetail);
        }
        return cateSaleList;
    }

    /**
     * 品类月销量排行 功能描述: <br>
     * 〈功能详细描述〉
     *
     * @param storeCode
     * @param month
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @Override
    public List<BiStoreCateSaleDetailDto> queryStoreCateSaleVolumeMonth(String storeCode, String month) {
        LOGGER.info("enter queryStoreCateSaleVolumeMonth, storeCode={},month={}", storeCode, month);
        BigDecimal saleTotal;
        Date time = DateUtils.getDateByStr(month, "yyyy-MM");
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("storeCode", storeCode);
        // 设置有效月份
        paramMap.put("month", DateUtils.getDateStrByFormat(time, "yyyyMM"));

        // 月销售数据
        BiStoreSaleMonthDto storeMonthSale = dalClient.queryForObject("BI_STORE_DATA_WAP.QUERY_STORE_SALE_MONTH",
                paramMap, BiStoreSaleMonthDto.class);
        if (null == storeMonthSale || StringUtils.isBlank(storeMonthSale.getSaleVolume())) {
            saleTotal = BigDecimal.ZERO;
        } else {
            saleTotal = DJStringUtils.bigDecimal(storeMonthSale.getSaleVolume());
        }

        // 查询
        List<BiStoreCateSaleDayDto> saleMonths = dalClient.queryForList(
                "BI_STORE_DATA_WAP.QUERY_STORE_CATE_VOLUME_RANK_MONTH", paramMap, BiStoreCateSaleDayDto.class);
        LOGGER.debug("queryStoreCateSaleVolumeMonth saleMonths={}", saleMonths);
        // 数据为空时
        if (CollectionUtils.isEmpty(saleMonths)) {
            return Collections.emptyList();
        }
        BiStoreCateSaleDetailDto cateDetail;
        BigDecimal saleAmount;
        String saleVolume;
        BigDecimal lastYearVolume;
        List<BiStoreCateSaleDetailDto> cateSaleList = new ArrayList<BiStoreCateSaleDetailDto>();
        for (BiStoreCateSaleDayDto saleMonth : saleMonths) {
            // 获取品类编码
            String cateCode = saleMonth.getCateCode();
            String cateName = ConfigConstants.DEFAULT_CATE_MAP.get(cateCode);
            // 判断品类编码和品类名称是否为空
            if (StringUtils.isBlank(cateCode) || StringUtils.isBlank(cateName)) {
                // 如果品类编码,或者品类名称不存在,则不处理该品类信息
                continue;
            }
            cateDetail = new BiStoreCateSaleDetailDto();
            cateDetail.setCateCode(cateCode);
            cateDetail.setCateName(cateName);
            if (StringUtils.isNotBlank(saleMonth.getSaleAmount())) {
                saleAmount = DJStringUtils.bigDecimal(saleMonth.getSaleAmount());
            } else {
                saleAmount = BigDecimal.ZERO;
            }
            // 销售额
            cateDetail.setSaleAmount(BiSaleAmountDto.valueOf(saleAmount));

            // 销量
            saleVolume = StringUtils.isBlank(saleMonth.getSaleVolume()) ? "0" : saleMonth.getSaleVolume();
            cateDetail.setSaleVolume(saleVolume);

            // 去年同期销量
            if (StringUtils.isNotBlank(saleMonth.getLastYearVolume())) {
                lastYearVolume = DJStringUtils.bigDecimal(saleMonth.getLastYearVolume());
            } else {
                lastYearVolume = BigDecimal.ZERO;
            }
            // 计算同期销量比例
            cateDetail.setSaleRate(DJStringUtils.calculateYoyString(DJStringUtils.bigDecimal(saleVolume),
                    lastYearVolume));

            // 计算品类占比
            cateDetail.setSaleTotalRate(DJStringUtils.calculateRateString(saleVolume, saleTotal.toString()));

            // 正向销量
            cateDetail.setPositiveSaleVolume(StringUtils.isBlank(saleMonth.getPositiveSaleVolume()) ? "0" : saleMonth
                    .getPositiveSaleVolume());

            // 逆向销量
            cateDetail.setNegativeSaleVolume(StringUtils.isBlank(saleMonth.getNegativeSaleVolume()) ? "0" : saleMonth
                    .getNegativeSaleVolume());

            cateSaleList.add(cateDetail);
        }
        return cateSaleList;
    }

    /**
     * 查询品类日销售详情 功能描述: <br>
     * 〈功能详细描述〉
     *
     * @param storeCode
     * @param cateCode
     * @param month
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @Override
    public List<BiStoreSaleDetailDto> queryCateSaleDetailByDay(String storeCode, String cateCode, String month) {
        LOGGER.info("enter queryCateSaleDetailByDay, storeCode={},cateCode={},month={}", new Object[] { storeCode,
                cateCode, month });
        // 详情数组
        List<BiStoreSaleDetailDto> detailList = new ArrayList<BiStoreSaleDetailDto>();
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("storeCode", storeCode);
        paramMap.put("cateCode", cateCode);
        List<String> days = getDaysOnMonth(month);
        // 设置有效天数
        paramMap.put("days", days);
        // 查询
        List<BiStoreSaleDayDto> saleDays = dalClient.queryForList("BI_STORE_DATA_WAP.QUERY_CATE_SALE_DETAIL_BY_DAYS",
                paramMap, BiStoreSaleDayDto.class);
        LOGGER.debug("queryCateSaleDetailByDay saleDays={}", saleDays);
        Map<String, BiStoreSaleDayDto> saleMap;
        // 将销售数据转换成MAP格式
        if (CollectionUtils.isNotEmpty(saleDays)) {
            saleMap = Maps.uniqueIndex(saleDays, new Function<BiStoreSaleDayDto, String>() {
                @Override
                public String apply(BiStoreSaleDayDto input) {
                    return input.getSaleDate();
                }
            });
        } else {
            saleMap = new HashMap<String, BiStoreSaleDayDto>();
        }
        BiStoreSaleDayDto saleDay;
        for (String day : days) {
            if (DateUtils.getDateStrByFormat(null, "yyyyMMdd").equals(day)) {
                // 添加当日实时数据
                detailList.add(getCurrDayCateSaleDetail(storeCode, cateCode));
            } else {
                saleDay = saleMap.get(day);
                if (null == saleDay) {
                    saleDay = new BiStoreSaleDayDto();
                    saleDay.setSaleDate(day);
                }
                detailList.add(convertSaleDayDetail(saleDay));
            }
        }
        return detailList;
    }

    /**
     * 查询品类月销售详情 功能描述: <br>
     * 〈功能详细描述〉
     *
     * @param storeCode
     * @param cateCode
     * @param year
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @Override
    public List<BiStoreSaleDetailDto> queryCateSaleDetailByMonth(String storeCode, String cateCode, String year) {
        LOGGER.info("enter queryCateSaleDetailByMonth, storeCode={},cateCode={},year={}", new Object[] { storeCode,
                cateCode, year });
        // 详情数组
        List<BiStoreSaleDetailDto> detailList = new ArrayList<BiStoreSaleDetailDto>();
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("storeCode", storeCode);
        paramMap.put("cateCode", cateCode);
        List<String> months = getMonthsOnYear(year);
        // 设置有效天数
        paramMap.put("months", months);
        // 查询
        List<BiStoreSaleDayDto> saleMonths = dalClient.queryForList(
                "BI_STORE_DATA_WAP.QUERY_CATE_SALE_DETAIL_BY_MONTHS", paramMap, BiStoreSaleDayDto.class);
        LOGGER.debug("queryCateSaleDetailByMonth saleMonths={}", saleMonths);
        Map<String, BiStoreSaleDayDto> saleMap;
        // 将销售数据转换成MAP格式
        if (CollectionUtils.isNotEmpty(saleMonths)) {
            saleMap = Maps.uniqueIndex(saleMonths, new Function<BiStoreSaleDayDto, String>() {
                @Override
                public String apply(BiStoreSaleDayDto input) {
                    return input.getSaleDate();
                }
            });
        } else {
            saleMap = new HashMap<String, BiStoreSaleDayDto>();
        }
        BiStoreSaleDayDto saleMonth;
        for (String month : months) {
            saleMonth = saleMap.get(month);
            if (null == saleMonth) {
                saleMonth = new BiStoreSaleDayDto();
                saleMonth.setSaleDate(month);
            }
            detailList.add(convertSaleMonthsDetail(saleMonth, storeCode, cateCode));
        }
        return detailList;
    }

    /**
     * 转换品类月销售详情 功能描述: <br>
     * 〈功能详细描述〉
     *
     * @param detail
     * @param storeCode
     * @param cateCode
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    private BiStoreSaleDetailDto convertSaleMonthsDetail(BiStoreSaleDayDto detail, String storeCode, String cateCode) {
        BiStoreSaleDetailDto dayDetail = new BiStoreSaleDetailDto();
        // 格式化时间
        Date saleDate = DateUtils.getDateByStr(detail.getSaleDate(), "yyyyMM");
        // 设置月份显示格式
        dayDetail.setSaleDate(DateUtils.getDateStrByFormat(saleDate, "yyyy-MM"));
        dayDetail.setRateDate(DateUtils.getDateStrByFormat(DateUtils.getNextYear(saleDate, -1), "yyyy年MM月"));
        BigDecimal saleAmount;
        // 总销售额
        if (StringUtils.isNotBlank(detail.getSaleAmount())) {
            saleAmount = DJStringUtils.bigDecimal(detail.getSaleAmount());
        } else {
            saleAmount = BigDecimal.ZERO;
        }
        BigDecimal positiveSaleAmount;
        // 正向销售额
        if (StringUtils.isNotBlank(detail.getPositiveSaleAmount())) {
            positiveSaleAmount = DJStringUtils.bigDecimal(detail.getPositiveSaleAmount());
        } else {
            positiveSaleAmount = BigDecimal.ZERO;
        }
        BigDecimal positiveSaleVolume;
        // 正向销售数量
        if (StringUtils.isNotBlank(detail.getPositiveSaleVolume())) {
            positiveSaleVolume = DJStringUtils.bigDecimal(detail.getPositiveSaleVolume());
        } else {
            positiveSaleVolume = BigDecimal.ZERO;
        }
        BigDecimal negativeSaleAmount;
        // 逆向销售额
        if (StringUtils.isNotBlank(detail.getNegativeSaleAmount())) {
            negativeSaleAmount = DJStringUtils.bigDecimal(detail.getNegativeSaleAmount());
        } else {
            negativeSaleAmount = BigDecimal.ZERO;
        }
        BigDecimal negativeSaleVolume;
        // 逆向销售数量
        if (StringUtils.isNotBlank(detail.getNegativeSaleVolume())) {
            negativeSaleVolume = DJStringUtils.bigDecimal(detail.getNegativeSaleVolume());
        } else {
            negativeSaleVolume = BigDecimal.ZERO;
        }
        BigDecimal lasYearSaleAmount;
        // 去年同期数据存在时
        if (StringUtils.isNotBlank(detail.getLastYearSaleAmount())) {
            lasYearSaleAmount = DJStringUtils.bigDecimal(detail.getLastYearSaleAmount());
        } else {
            lasYearSaleAmount = BigDecimal.ZERO;
        }
        
        BigDecimal jxcSaleAmount;
        // 进销差销售额
        if (StringUtils.isNotBlank(detail.getJxcAmount())) {
            jxcSaleAmount = DJStringUtils.bigDecimal(detail.getJxcAmount());
        } else {
            jxcSaleAmount = BigDecimal.ZERO;
        }

        // 本月销售数据 ,需要加当日实时销售销售
        if (DateUtils.getThisMonth().equals(detail.getSaleDate())) {
            // 取门店实时数据
            Map<String, Object> storeSaleRedis = biKafkaRealTimeSaleService.getStoreCateSaleDataFromRedis(storeCode,
                    cateCode);
            // 门店去年同期销售金额
            if (null != storeSaleRedis) {
                // 总销售额
                if (null != storeSaleRedis.get("payAmt")) {
                    saleAmount = saleAmount.add(DJStringUtils.bigDecimal(String.valueOf(storeSaleRedis.get("payAmt"))));
                }
                // 正向销售额
                if (null != storeSaleRedis.get("payAmt_f")) {
                    positiveSaleAmount = positiveSaleAmount.add(DJStringUtils.bigDecimal(String.valueOf(storeSaleRedis
                            .get("payAmt_f"))));
                }
                // 正向销售数量
                if (null != storeSaleRedis.get("saleCnt_f")) {
                    positiveSaleVolume = positiveSaleVolume.add(DJStringUtils.bigDecimal(String.valueOf(storeSaleRedis
                            .get("saleCnt_f"))));
                }
                // 逆向销售额
                if (null != storeSaleRedis.get("payAmt_r")) {
                    negativeSaleAmount = negativeSaleAmount.add(DJStringUtils.bigDecimal(String.valueOf(storeSaleRedis
                            .get("payAmt_r"))));
                }
                // 逆向销售数量
                if (null != storeSaleRedis.get("saleCnt_r")) {
                    negativeSaleVolume = negativeSaleVolume.add(DJStringUtils.bigDecimal(String.valueOf(storeSaleRedis
                            .get("saleCnt_r"))));
                }
				
            }
            
            // 月初
            if ("01".equals(DateUtils.getDateStrByFormat(new Date(), "dd"))) {
                dayDetail.setSystemTime("暂未统计");
            } else {
                // 昨日时间
                dayDetail.setSystemTime("截至" + DateUtils.getDateStrByFormat(DateUtils.getNextDay(new Date(), -1), "MM-dd"));
            }
        }else{
            dayDetail.setSystemTime("截至" + DateUtils.getDateStrByFormat( DateUtils.getLastDateOfMonth(saleDate), "MM-dd"));
        }
        // 设置销售额, 正向, 逆向销售额销量
        dayDetail.setSaleAmount(BiSaleAmountDto.valueOf(saleAmount));

        // 增加展示万元单位销售额
        dayDetail.setSaleAmountShow(BiSaleAmountDto.valueOf(saleAmount, BiSaleAmountDto.UNIT_WAN));

        dayDetail.setPositiveSaleAmount(BiSaleAmountDto.valueOf(positiveSaleAmount));
        dayDetail.setPositiveSaleVolume(String.valueOf(positiveSaleVolume.longValue()));
        dayDetail.setNegativeSaleAmount(BiSaleAmountDto.valueOf(negativeSaleAmount));
        dayDetail.setNegativeSaleVolume(String.valueOf(negativeSaleVolume.longValue()));
        dayDetail.setJxcAmount(BiSaleAmountDto.valueOf(jxcSaleAmount));
        // 同比
        dayDetail.setSaleRate(DJStringUtils.calculateYoyString(saleAmount, lasYearSaleAmount));
        
        return dayDetail;
    }

    /**
     * 门店月份完成率 功能描述: <br>
     * 〈功能详细描述〉
     *
     * @param month
     * @param storeCode
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @Override
    public BiStoreSaleCompleteRateDto queryStoreCompleteRate(String month, String storeCode, String storeName) {
        LOGGER.info("enter queryStoreCompleteRate, storeCode={},storeName={},month={}", new Object[] { storeCode,
                storeName, month });
        BiStoreSaleCompleteRateDto storeComplete = new BiStoreSaleCompleteRateDto();
        // 转换时间
        Date time = DateUtils.getDateByStr(month, "yyyy-MM");
        // 转换成数据库月份格式
        String monthDb = DateUtils.getDateStrByFormat(time, "yyyyMM");
        // 取当月的销售任务金额，毛利任务金额
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("month", monthDb);
        paramMap.put("storeCode", storeCode);
        BiStoreMonSaleTask task = dalClient.queryForObject("BI_STORE_DATA_WAP.QUERY_STORE_TASK_TARGET", paramMap,
                BiStoreMonSaleTask.class);
        LOGGER.debug("queryStoreCompleteRate task={}", task);
        // 品类任务目标
        List<BiStoreCateMonSaleTask> cateTasks = dalClient.queryForList("BI_STORE_DATA_WAP.QUERY_CATE_TASK_TARGET",
                paramMap, BiStoreCateMonSaleTask.class);
        LOGGER.debug("queryStoreCompleteRate cateTasks={}", cateTasks);
        // 判断不是本月
        if (!DateUtils.getThisMonth().equals(monthDb)) {
            BigDecimal saleAmount;
            BigDecimal taskSaleAmount;
            // 任务不为空时
            if (null != task && StringUtils.isNotBlank(task.getStoreCode())) {
                storeComplete.setStoreName(storeName);
                // 目标任务不为空
                if (null != task.getTaskSaleAmount()) {
                    taskSaleAmount = task.getTaskSaleAmount();
                } else {
                    taskSaleAmount = BigDecimal.ZERO;
                }
                // 实际销售额不为空
                if (null != task.getSaleAmount()) {
                    saleAmount = task.getSaleAmount();
                } else {
                    saleAmount = BigDecimal.ZERO;
                }
                // 设置目标销售额,实际销售额
                storeComplete.setTargetSaleAmount(BiSaleAmountDto.valueOf(taskSaleAmount));
                storeComplete.setSaleAmount(BiSaleAmountDto.valueOf(saleAmount));
                // 差额小于0时, 默认0
                if (taskSaleAmount.subtract(saleAmount).intValue() < 0) {
                    storeComplete.setSurplusAmount(BiSaleAmountDto.zero());
                } else {
                    storeComplete.setSurplusAmount(BiSaleAmountDto.valueOf(taskSaleAmount.subtract(saleAmount)));
                }
                // 占比
                storeComplete.setRate(DJStringUtils.calculateRateString(saleAmount.toString(),
                        taskSaleAmount.toString()));
            } else {
                // 设置门店名称
                storeComplete.setStoreName(storeName);
                storeComplete.setTargetSaleAmount(BiSaleAmountDto.zero());
                storeComplete.setSurplusAmount(BiSaleAmountDto.zero());
                // 查询实际的销售额
                // 月销售数据
                BiStoreSaleMonthDto storeMonthSale = dalClient.queryForObject(
                        "BI_STORE_DATA_WAP.QUERY_STORE_SALE_MONTH", paramMap, BiStoreSaleMonthDto.class);
                // 月销售额不为空时
                if (null != storeMonthSale && StringUtils.isNotBlank(storeMonthSale.getSaleAmount())) {
                    storeComplete.setSaleAmount(BiSaleAmountDto.valueOf(DJStringUtils.bigDecimal(storeMonthSale
                            .getSaleAmount())));
                } else {
                    storeComplete.setSaleAmount(BiSaleAmountDto.zero());
                }
                storeComplete.setRate("0.0");
            }
            BiStoreCateSaleCompleteRateDto cateRate;
            BiStoreCateMonSaleTask cateTask;
            // 品类完成度集合
            List<BiStoreCateSaleCompleteRateDto> cateSales = new ArrayList<BiStoreCateSaleCompleteRateDto>();
            Map<String, BiStoreCateMonSaleTask> cateTaskMap;
            // 品类任务目标不为空时
            if (!CollectionUtils.isEmpty(cateTasks)) {
                // 转换成Map
                cateTaskMap = Maps.uniqueIndex(cateTasks, new Function<BiStoreCateMonSaleTask, String>() {
                    @Override
                    public String apply(BiStoreCateMonSaleTask input) {
                        return input.getCateCode();
                    }
                });
            } else {
                cateTaskMap = new HashMap<String, BiStoreCateMonSaleTask>();
            }
            BigDecimal cateSaleAmount;
            BigDecimal cateTaskAmount;
            // 循环8个品类数据
            for (Map.Entry<String, String> cate : ConfigConstants.DEFAULT_CATE_MAP.entrySet()) {
                cateRate = new BiStoreCateSaleCompleteRateDto();
                cateRate.setCateCode(cate.getKey());
                cateRate.setCateName(cate.getValue());
                cateTask = cateTaskMap.get(cate.getKey());
                // 有品类任务时
                if (null != cateTask) {
                    // 判断目标额
                    if (null != cateTask.getTaskSaleAmount()) {
                        cateTaskAmount = cateTask.getTaskSaleAmount();
                    } else {
                        cateTaskAmount = BigDecimal.ZERO;
                    }
                    cateRate.setTargetSaleAmount(BiSaleAmountDto.valueOf(cateTaskAmount));

                    // 查询实际销售额
                    paramMap.put("cateCode", cate.getKey());
                    BiStoreCateSaleDayDto cateMonthSale = dalClient.queryForObject(
                            "BI_STORE_DATA_WAP.QUERY_STORE_CATE_AMOUNT_RANK_MONTH", paramMap,
                            BiStoreCateSaleDayDto.class);
                    // 数据存在时
                    if (null != cateMonthSale && StringUtils.isNotBlank(cateMonthSale.getSaleAmount())) {
                        cateSaleAmount = DJStringUtils.bigDecimal(cateMonthSale.getSaleAmount());
                    } else {
                        // 默认
                        cateSaleAmount = BigDecimal.ZERO;
                    }

                    cateRate.setSaleAmount(BiSaleAmountDto.valueOf(cateSaleAmount));
                    // 判断差额
                    if (cateTaskAmount.subtract(cateSaleAmount).intValue() < 0) {
                        cateRate.setSurplusAmount(BiSaleAmountDto.zero());
                    } else {
                        cateRate.setSurplusAmount(BiSaleAmountDto.valueOf(cateTaskAmount.subtract(cateSaleAmount)));
                    }
                    // 计算完成率
                    cateRate.setRate(DJStringUtils.calculateRateString(cateSaleAmount.toString(),
                            cateTaskAmount.toString()));
                } else {
                    // 没有任务目标, 默认为0
                    cateRate.setTargetSaleAmount(BiSaleAmountDto.zero());
                    cateRate.setSurplusAmount(BiSaleAmountDto.zero());
                    cateRate.setRate("0.0");
                    // 查询实际销售额
                    paramMap.put("cateCode", cate.getKey());
                    BiStoreCateSaleDayDto cateMonthSale = dalClient.queryForObject(
                            "BI_STORE_DATA_WAP.QUERY_STORE_CATE_AMOUNT_RANK_MONTH", paramMap,
                            BiStoreCateSaleDayDto.class);
                    // 数据存在时
                    if (null != cateMonthSale && StringUtils.isNotBlank(cateMonthSale.getSaleAmount())) {
                        cateRate.setSaleAmount(BiSaleAmountDto.valueOf(DJStringUtils.bigDecimal(cateMonthSale
                                .getSaleAmount())));
                    } else {
                        // 默认
                        cateRate.setSaleAmount(BiSaleAmountDto.zero());
                    }
                }
                // 添加品类数据
                cateSales.add(cateRate);
            }
            // 根据完成率排序
            Collections.sort(cateSales, new Comparator<BiStoreCateSaleCompleteRateDto>() {
                @Override
                public int compare(BiStoreCateSaleCompleteRateDto o1, BiStoreCateSaleCompleteRateDto o2) {
                    // 完成率相同时， 暂不考虑单位
                    if (o2.getRate().equals(o1.getRate())) {
                        return DJStringUtils.bigDecimal(o2.getSaleAmount().getAmount()).compareTo(
                                DJStringUtils.bigDecimal(o1.getSaleAmount().getAmount()));
                    }
                    // 降序
                    return DJStringUtils.bigDecimal(o2.getRate()).compareTo(DJStringUtils.bigDecimal(o1.getRate()));
                }
            });
            // 设置品类销售完成率
            storeComplete.setCateSales(cateSales);
        } else {
            // 当前月门店销售数据
            // 取门店实时数据
            Map<String, Object> storeSaleRedis = biKafkaRealTimeSaleService.getStoreSaleDataFromRedis(storeCode);
            // 当日销售额
            BigDecimal saleAmount;
            if (null != storeSaleRedis && null != storeSaleRedis.get("payAmt")) {
                saleAmount = DJStringUtils.bigDecimal(String.valueOf(storeSaleRedis.get("payAmt")));
            } else {
                saleAmount = BigDecimal.ZERO;
            }
            // 月销售数据
            BiStoreSaleMonthDto storeMonthSale = dalClient.queryForObject("BI_STORE_DATA_WAP.QUERY_STORE_SALE_MONTH",
                    paramMap, BiStoreSaleMonthDto.class);
            // 月销售额不为空时,加上日销售额
            if (null != storeMonthSale && StringUtils.isNotBlank(storeMonthSale.getSaleAmount())) {
                saleAmount = saleAmount.add(DJStringUtils.bigDecimal(storeMonthSale.getSaleAmount()));
            }
            // 当前月总的销售额
            storeComplete.setSaleAmount(BiSaleAmountDto.valueOf(saleAmount));

            BigDecimal taskSaleAmount;
            // 任务不为空时
            if (null != task && StringUtils.isNotBlank(task.getStoreCode())) {
                storeComplete.setStoreName(storeName);
                // 目标任务不为空
                if (null != task.getTaskSaleAmount()) {
                    taskSaleAmount = task.getTaskSaleAmount();
                } else {
                    taskSaleAmount = BigDecimal.ZERO;
                }
                // 设置目标销售额,实际销售额
                storeComplete.setTargetSaleAmount(BiSaleAmountDto.valueOf(taskSaleAmount));
                // 差额小于0时, 默认0
                if (taskSaleAmount.subtract(saleAmount).intValue() < 0) {
                    storeComplete.setSurplusAmount(BiSaleAmountDto.zero());
                } else {
                    storeComplete.setSurplusAmount(BiSaleAmountDto.valueOf(taskSaleAmount.subtract(saleAmount)));
                }
                // 占比
                storeComplete.setRate(DJStringUtils.calculateRateString(saleAmount.toString(),
                        taskSaleAmount.toString()));
            } else {
                // 设置门店名称
                storeComplete.setStoreName(storeName);
                storeComplete.setTargetSaleAmount(BiSaleAmountDto.zero());
                storeComplete.setSurplusAmount(BiSaleAmountDto.zero());
                storeComplete.setRate("0.0");
            }
            BiStoreCateSaleCompleteRateDto cateRate;
            BiStoreCateMonSaleTask cateTask;
            // 品类完成度集合
            List<BiStoreCateSaleCompleteRateDto> cateSales = new ArrayList<BiStoreCateSaleCompleteRateDto>();
            Map<String, BiStoreCateMonSaleTask> cateTaskMap;
            // 品类任务目标不为空时
            if (!CollectionUtils.isEmpty(cateTasks)) {
                // 转换成Map
                cateTaskMap = Maps.uniqueIndex(cateTasks, new Function<BiStoreCateMonSaleTask, String>() {
                    @Override
                    public String apply(BiStoreCateMonSaleTask input) {
                        return input.getCateCode();
                    }
                });
            } else {
                cateTaskMap = new HashMap<String, BiStoreCateMonSaleTask>();
            }
            BigDecimal cateSaleAmount;
            BigDecimal cateTaskAmount;
            BigDecimal cateRealAmount;
            // 循环8个品类数据
            for (Map.Entry<String, String> cate : ConfigConstants.DEFAULT_CATE_MAP.entrySet()) {
                cateRate = new BiStoreCateSaleCompleteRateDto();
                cateRate.setCateCode(cate.getKey());
                cateRate.setCateName(cate.getValue());
                // 对应品类的目标
                cateTask = cateTaskMap.get(cate.getKey());
                // 有品类任务时
                if (null != cateTask && null != cateTask.getTaskSaleAmount()) {
                    cateTaskAmount = cateTask.getTaskSaleAmount();
                } else {
                    cateTaskAmount = BigDecimal.ZERO;
                }
                cateRate.setTargetSaleAmount(BiSaleAmountDto.valueOf(cateTaskAmount));

                // 判断实际销售额
                // 查询实际销售额
                paramMap.put("cateCode", cate.getKey());
                BiStoreCateSaleDayDto cateMonthSale = dalClient.queryForObject(
                        "BI_STORE_DATA_WAP.QUERY_STORE_CATE_AMOUNT_RANK_MONTH", paramMap, BiStoreCateSaleDayDto.class);
                // 品类月数据存在时
                if (null != cateMonthSale && StringUtils.isNotBlank(cateMonthSale.getSaleAmount())) {
                    cateSaleAmount = DJStringUtils.bigDecimal(cateMonthSale.getSaleAmount());
                } else {
                    // 默认
                    cateSaleAmount = BigDecimal.ZERO;
                }
                // 取品类的实时销售数据
                cateRealAmount = biKafkaRealTimeSaleService.queryStoreCateDaySale(storeCode, cate.getKey());

                if (null != cateRealAmount) {
                    cateSaleAmount = cateSaleAmount.add(cateRealAmount);
                }
                // 设置品类月销售额
                cateRate.setSaleAmount(BiSaleAmountDto.valueOf(cateSaleAmount));
                // 判断差额
                if (cateTaskAmount.subtract(cateSaleAmount).intValue() < 0) {
                    cateRate.setSurplusAmount(BiSaleAmountDto.zero());
                } else {
                    cateRate.setSurplusAmount(BiSaleAmountDto.valueOf(cateTaskAmount.subtract(cateSaleAmount)));
                }
                // 计算完成率
                cateRate.setRate(DJStringUtils.calculateRateString(cateSaleAmount.toString(), cateTaskAmount.toString()));
                // 添加品类数据
                cateSales.add(cateRate);
            }
            // 根据完成率排序
            Collections.sort(cateSales, new Comparator<BiStoreCateSaleCompleteRateDto>() {
                @Override
                public int compare(BiStoreCateSaleCompleteRateDto o1, BiStoreCateSaleCompleteRateDto o2) {
                    // 完成率相同时， 暂不考虑单位
                    if (o2.getRate().equals(o1.getRate())) {
                        return DJStringUtils.bigDecimal(o2.getSaleAmount().getAmount()).compareTo(
                                DJStringUtils.bigDecimal(o1.getSaleAmount().getAmount()));
                    }
                    // 降序
                    return DJStringUtils.bigDecimal(o2.getRate()).compareTo(DJStringUtils.bigDecimal(o1.getRate()));
                }
            });
            // 设置品类销售完成率
            storeComplete.setCateSales(cateSales);
        }
        return storeComplete;
    }

    /**
     * 查询同大区销售完成率排行 功能描述: <br>
     * 〈功能详细描述〉
     *
     * @param month
     * @param regionCode
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @Override
    public BiStoreSaleCompletaRateRankingDto queryStoreCompleteRateRanking(String month, String regionCode,
            String storeCode, String storeName) {
        LOGGER.info("enter queryStoreCompleteRate, storeCode={},storeName={},month={},regionCode={}", new Object[] {
                storeCode, storeName, month, regionCode });
        BiStoreSaleCompletaRateRankingDto rateRanking = new BiStoreSaleCompletaRateRankingDto();
        rateRanking.setStoreCode(storeCode);
        rateRanking.setStoreName(storeName);
        // 转换时间
        Date time = DateUtils.getDateByStr(month, "yyyy-MM");
        // 转换成数据库月份格式
        String monthDb = DateUtils.getDateStrByFormat(time, "yyyyMM");
        // 判断是否是当前月
        if (DateUtils.getThisMonth().equals(monthDb)) {
            // 昨天的日期
            rateRanking.setUpdateTime(DateUtils.getDateStrByFormat(
                    DateUtils.getLastDateOfDay(DateUtils.getNextDay(new Date(), -1)), ""));
        } else {
            // 月末最后一天
            rateRanking.setUpdateTime(DateUtils.getLastDateOfMonthStr(time, "yyyy-MM-dd HH:mm:ss"));
        }

        BiStoreSaleCompleteDto storeRanking = null;
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("month", monthDb);
        paramMap.put("regionCode", regionCode);
        List<BiStoreSaleCompleteDto> storeRankings = dalClient.queryForList(
                "BI_STORE_DATA_WAP.QUERY_STORE_SALE_COMPLETE_RATE_RANKING", paramMap, BiStoreSaleCompleteDto.class);
        LOGGER.debug("queryStoreCompleteRate storeRankings={}", storeRankings);
        // 判断销售排行是否为空
        if (CollectionUtils.isEmpty(storeRankings)) {
            rateRanking.setRate("0.0");
        } else {
            int ranking = 0;
            for (BiStoreSaleCompleteDto saleRate : storeRankings) {
                ranking++;
                // 设置排名
                saleRate.setRanking(ranking);
                // 转换完成率
                if (StringUtils.isNotBlank(saleRate.getRate())) {
                    // 乘100,取一位小数
                    saleRate.setRate(DJStringUtils.bigDecimal(saleRate.getRate()).multiply(BigDecimal.valueOf(100l))
                            .setScale(1, BigDecimal.ROUND_DOWN).toString());
                } else {
                    saleRate.setRate("0.0");
                }
                // 判断门店编码是否一致
                if (storeCode.equals(saleRate.getStoreCode())) {
                    storeRanking = saleRate;
                    rateRanking.setRanking(ranking);
                    rateRanking.setRate(saleRate.getRate());
                }
            }
            // 该门店数据不存在
            if (null == storeRanking) {
                // 增加门店
                ranking++;
                rateRanking.setRate("0.0");
                rateRanking.setRanking(ranking);
                storeRanking = new BiStoreSaleCompleteDto();
                storeRanking.setStoreCode(storeCode);
                storeRanking.setStoreName(storeName);
                storeRanking.setRate("0.0");
                storeRanking.setRanking(ranking);
                storeRankings.add(storeRanking);
            } else {
                // 打败多少个门店总数-排名
                rateRanking.setBeatNum(ranking - rateRanking.getRanking());
            }
            // 设置排名
            rateRanking.setStoreRanking(storeRankings);
        }
        return rateRanking;
    }

    /**
     * 转换品类数据
     */
    @Override
    public List<SaleCategory> getCategory(List<Category> cates, String cateCode) {
        if (CollectionUtils.isNotEmpty(cates)) {
            SaleCategory saleCate;
            List<SaleCategory> list = new ArrayList<SaleCategory>();
            for (Category cate : cates) {
                saleCate = new SaleCategory();
                saleCate.setCateCode(cate.getCategoryCode());
                saleCate.setCateName(cate.getCategoryName());
                saleCate.setChooseStatus(SaleCategory.ChooseStatus.NO);
                if (null != cate.getCategoryCode() && cate.getCategoryCode().equals(cateCode)) {
                    saleCate.setChooseStatus(SaleCategory.ChooseStatus.YES);
                } else {
                    saleCate.setChooseStatus(SaleCategory.ChooseStatus.NO);
                }
                list.add(saleCate);
            }
            return list;
        }
        return Collections.emptyList();
    }

    /**
     * 日维度店长查询门店以及品类平均客单价
     */
    @Override
    public BiStoreSaleCustAvgPricedDto queryCustAvgPriceByDay(StoreManDto storeManDto, String day) {
        BiStoreSaleCustAvgPricedDto dto = new BiStoreSaleCustAvgPricedDto();
        // 1.品类列表(品类客单价列表)
        List<BiStoreSaleCustAvgPricedCateDto> cateList = new ArrayList<BiStoreSaleCustAvgPricedCateDto>();
        // 2.门店当前客单价
        BigDecimal currCustAvgPrice = BigDecimal.ZERO;
        // 3.门店购买人数
        String custCount = "0";
        // 4.门店平均客单价同比
        String saleRate = "0.0";
        // 5.门店销售金额(日总销售额)
        BigDecimal totalAmount = BigDecimal.ZERO;

        Map<String, Object> paramMap = new HashMap<String, Object>();
        // 初始化8个品类
        List<String> cates = new ArrayList<String>();
        cates.addAll(ConfigConstants.DEFAULT_CATE_MAP.keySet());

        // 判断是否是查询今天的客单价
        if (DateUtils.getDateStrByFormat(null, "yyyy-MM-dd").equals(day)) {
            // 查询当天门店销售数据
            Map<String, Object> todaySale = biKafkaRealTimeSaleService.getStoreSaleDataFromRedis(storeManDto
                    .getStoreCode());
            if (null != todaySale) {
                if (null != todaySale.get("payAmt")) {
                    totalAmount = DJStringUtils.bigDecimal(String.valueOf(todaySale.get("payAmt")));
                }
                if (null != todaySale.get("memberCount")) {
                    custCount = String.valueOf(todaySale.get("memberCount"));
                }
            }
            // 实时客单价
            currCustAvgPrice = DJStringUtils.divide(totalAmount, DJStringUtils.bigDecimal(custCount));
            // 去年同期客单价
            BigDecimal custAvgPriceLastYear = biKafkaRealTimeSaleService.queryLastYearStoreCustAvgPrice(storeManDto
                    .getStoreCode());
            // 客单价同比
            saleRate = DJStringUtils.calculateYoyString(currCustAvgPrice, custAvgPriceLastYear);

            // 八大品类
            // 查询门店品牌实时销售数据
            List<Map<String, Object>> cateSales = biKafkaRealTimeSaleService.getStoreCateSaleDataFromRedis(storeManDto
                    .getStoreCode());
            if (CollectionUtils.isNotEmpty(cateSales)) {
                BigDecimal cateTotalSaleNum;
                String cateSaleRate;
                String cateCustCount;
                BiSaleAmountDto cateCurrCustAvgPrice;
                for (Map<String, Object> sale : cateSales) {
                    // 获取品类编码
                    String cateCode = String.valueOf(sale.get("deptCd"));
                    String cateName = ConfigConstants.DEFAULT_CATE_MAP.get(cateCode);
                    // 如果品类编码,或者品类名称不存在,则不处理该品类信息(仅八大品类)
                    if (StringUtils.isBlank(cateCode) || StringUtils.isBlank(cateName)) {
                        continue;
                    }
                    if (null != sale.get("payAmt")) {
                        cateTotalSaleNum = DJStringUtils.bigDecimal(String.valueOf(sale.get("payAmt")));
                    } else {
                        cateTotalSaleNum = BigDecimal.ZERO;
                    }
                    // 购买人数
                    if (null != sale.get("memberCount")) {
                        cateCustCount = DJStringUtils.formatVolume(sale.get("memberCount"));
                    } else {
                        cateCustCount = "0";
                    }
                    // 实时客单价
                    cateCurrCustAvgPrice = BiSaleAmountDto.valueOfLow(DJStringUtils.divide(cateTotalSaleNum,
                            DJStringUtils.bigDecimal(cateCustCount)));
                    // 门店品类客单价同比
                    BigDecimal cateCustAvgPriceLstYear = biKafkaRealTimeSaleService.getStoreCateCustAvgPriceYoy(
                            storeManDto.getStoreCode(), cateCode);
                    if (null != cateCustAvgPriceLstYear) {
                        cateSaleRate = String.valueOf(cateCustAvgPriceLstYear);
                    } else {
                        cateSaleRate = "0.0";
                    }
                    cateList.add(new BiStoreSaleCustAvgPricedCateDto(cateCode, cateName, cateCurrCustAvgPrice,
                            DJStringUtils.transIntergerStr(cateCustCount), cateSaleRate, BiSaleAmountDto.valueOf(cateTotalSaleNum)));
                }
            }

        } else {
            // 门店信息
            Date time = DateUtils.getDateByStr(day, "yyyy-MM-dd");
            paramMap.clear();
            paramMap.put("storeCode", storeManDto.getStoreCode());
            paramMap.put("day", DateUtils.getDateStrByFormat(time, "yyyyMMdd"));
            BiStoreSaleDataDayDto saleData = dalClient.queryForObject("BI_STORE_DATA_WAP.QUERY_STORE_DATA_BY_DAY",
                    paramMap, BiStoreSaleDataDayDto.class);
            if (null != saleData) {
                if (StringUtils.isNotBlank(saleData.getSaleAmount())) {
                    totalAmount = DJStringUtils.bigDecimal(saleData.getSaleAmount());
                }
                if (StringUtils.isNotBlank(saleData.getCustAvgPrice())) {
                    currCustAvgPrice = DJStringUtils.bigDecimal(saleData.getCustAvgPrice());
                }
                if (StringUtils.isNotBlank(saleData.getCustNum())) {
                    custCount = saleData.getCustNum();
                }
                if (StringUtils.isNotBlank(saleData.getLastYearSaleAmount()) && StringUtils.isNotBlank(saleData.getLastYearCustNum())) {
                    BigDecimal lastYearCustAvgPrice = DJStringUtils.divide(
                            DJStringUtils.bigDecimal(saleData.getLastYearSaleAmount()),
                            DJStringUtils.bigDecimal(saleData.getLastYearCustNum()));
                    saleRate = DJStringUtils.calculateYoyString(currCustAvgPrice, lastYearCustAvgPrice);
                }
            }
            // 八大品类信息
            paramMap.clear();
            paramMap.put("storeCode", storeManDto.getStoreCode());
            paramMap.put("day", DateUtils.getDateStrByFormat(time, "yyyyMMdd"));
            paramMap.put("cates", cates);
            List<BiStoreCateSaleDataDayDto> cateSales = dalClient.queryForList(
                    "BI_STORE_DATA_WAP.QUERY_STORE_CATE_DATA_BY_DAY", paramMap, BiStoreCateSaleDataDayDto.class);
            BigDecimal cateCurrCustAvgPrice;
            String cateCustCount;
            if (CollectionUtils.isNotEmpty(cateSales)) {
                for (BiStoreCateSaleDataDayDto info : cateSales) {
                    String cateCode = info.getCateCode();
                    String cateName = ConfigConstants.DEFAULT_CATE_MAP.get(cateCode);
                    if (StringUtils.isBlank(cateCode) || StringUtils.isBlank(cateName)) {
                        continue;
                    }
                    if (StringUtils.isNotBlank(info.getCustAvgPrice())) {
                        cateCurrCustAvgPrice = DJStringUtils.bigDecimal(info.getCustAvgPrice());
                    } else {
                        cateCurrCustAvgPrice = BigDecimal.ZERO;
                    }
                    if (StringUtils.isNotBlank(info.getCustNum())) {
                        cateCustCount = info.getCustNum();
                    } else {
                        cateCustCount = "0";
                    }
                    // 品类客单价同比
                    String cateSaleRate = "0.0";
                    if (StringUtils.isNotBlank(info.getLastYearSaleAmount()) && StringUtils.isNotBlank(info.getLastYearCustNum())) {
                        BigDecimal lastYearCateCustAvgPrice = DJStringUtils.divide(
                                DJStringUtils.bigDecimal(info.getLastYearSaleAmount()),
                                DJStringUtils.bigDecimal(info.getLastYearCustNum()));
                        cateSaleRate = DJStringUtils.calculateYoyString(cateCurrCustAvgPrice, lastYearCateCustAvgPrice);
                    }
                    BiSaleAmountDto cateTotalSaleNum = BiSaleAmountDto.valueOf(DJStringUtils.bigDecimal(info
                            .getSaleAmount()));
                    cateList.add(new BiStoreSaleCustAvgPricedCateDto(cateCode, cateName, BiSaleAmountDto.valueOfLow(cateCurrCustAvgPrice),
                            DJStringUtils.transIntergerStr(cateCustCount), cateSaleRate, cateTotalSaleNum));
                }
            }

        }
        // 根据客单价排序
        Collections.sort(cateList, new Comparator<BiStoreSaleCustAvgPricedCateDto>() {
            @Override
            public int compare(BiStoreSaleCustAvgPricedCateDto o1, BiStoreSaleCustAvgPricedCateDto o2) {
                BigDecimal avgPrice1 = o1.getCurrCustAvgPrice().getSortMoney();
                BigDecimal avgPrice2 = o2.getCurrCustAvgPrice().getSortMoney();
                if (null != avgPrice1 && null != avgPrice2) {
                    return avgPrice2.compareTo(avgPrice1);
                } else if (null == avgPrice2) {
                    return -1;
                } else if (null == avgPrice1) {
                    return 1;
                }
                // 降序
                return 0;
            }
        });
        dto.setCateList(cateList);
        dto.setSaleRate(saleRate);
        dto.setCustCount(DJStringUtils.transIntergerStr(custCount));
        dto.setCurrCustAvgPrice(BiSaleAmountDto.valueOfLow(currCustAvgPrice));
        dto.setTotalSaleNum(BiSaleAmountDto.valueOf(totalAmount));
        return dto;
    }

    /**
     * 月维度店长查询门店以及品类平均客单价
     */
    @Override
    public BiStoreSaleCustAvgPricedDto queryCustAvgPriceByMonth(StoreManDto storeManDto, String month) {
        BiStoreSaleCustAvgPricedDto dto = new BiStoreSaleCustAvgPricedDto();

        String storeCode = storeManDto.getStoreCode();
        // 转换时间,数据库月份格式
        String monthDb = DateUtils.getDateStrByFormat(DateUtils.getDateByStr(month, "yyyy-MM"), "yyyyMM");

        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("month", monthDb);
        paramMap.put("storeCode", storeCode);

        // 客单价同比
        String saleRate = "0.0";
        String custCount = "0";
        BigDecimal currCustAvgPrice = BigDecimal.ZERO;
        BigDecimal totalAmount = BigDecimal.ZERO;
        List<BiStoreSaleCustAvgPricedCateDto> cateList = new ArrayList<BiStoreSaleCustAvgPricedCateDto>();

        // 月销售数据
        BiStoreSaleMonthDto storeMonthSale = dalClient.queryForObject("BI_STORE_DATA_WAP.QUERY_STORE_SALE_MONTH",
                paramMap, BiStoreSaleMonthDto.class);
        if (null != storeMonthSale) {
            // 总销售额
            if (StringUtils.isNotBlank(storeMonthSale.getSaleAmount())) {
                totalAmount = DJStringUtils.bigDecimal(storeMonthSale.getSaleAmount());
            } else {
                totalAmount = BigDecimal.ZERO;
            }
            // 购买人数
            if (StringUtils.isNotBlank(storeMonthSale.getCustNum())) {
                custCount = storeMonthSale.getCustNum();
            } else {
                custCount = "0";
            }
            // 客单价
            currCustAvgPrice = DJStringUtils.divide(totalAmount, DJStringUtils.bigDecimal(custCount));
            // 客单价同比
            if (StringUtils.isNotBlank(storeMonthSale.getLastYearSale()) && StringUtils.isNotBlank(storeMonthSale.getLastYearCustNum())) {
                saleRate = DJStringUtils.calculateYoyString(
                        currCustAvgPrice,
                        DJStringUtils.divide(DJStringUtils.bigDecimal(storeMonthSale.getLastYearSale()),
                                DJStringUtils.bigDecimal(storeMonthSale.getLastYearCustNum())));
            } else {
                saleRate = "0.0";
            }
        }

        // 查询品类
        List<BiStoreCateSaleDayDto> saleMonths = dalClient.queryForList(
                "BI_STORE_DATA_WAP.QUERY_STORE_CATE_AMOUNT_RANK_MONTH", paramMap, BiStoreCateSaleDayDto.class);
        Map<String, BiStoreCateSaleDayDto> cateSaleMonthMap = new HashMap<String, BiStoreCateSaleDayDto>();
        for (BiStoreCateSaleDayDto cateDay : saleMonths) {
            if (null != cateDay && StringUtils.isNotBlank(cateDay.getCateCode())) {
                cateSaleMonthMap.put(cateDay.getCateCode(), cateDay);
            }
        }

        BigDecimal cateCustAvgPrice = BigDecimal.ZERO;
        BigDecimal cateTotalAmount = BigDecimal.ZERO;
        String cateCustCount = "0";
        String cateSaleRate = "0";
        if (CollectionUtils.isNotEmpty(saleMonths)) {
            for (BiStoreCateSaleDayDto saleMonth : saleMonths) {
                String cateCode = saleMonth.getCateCode();
                String cateName = ConfigConstants.DEFAULT_CATE_MAP.get(cateCode);
                // 如果品类编码,或者品类名称不存在,则不处理该品类信息
                if (StringUtils.isBlank(cateCode) || StringUtils.isBlank(cateName)) {
                    continue;
                }
                // 销售额
                if (StringUtils.isNotBlank(saleMonth.getSaleAmount())) {
                    cateTotalAmount = DJStringUtils.bigDecimal(saleMonth.getSaleAmount());
                } else {
                    cateTotalAmount = BigDecimal.ZERO;
                }
                // 购买人数
                if (StringUtils.isNotBlank(saleMonth.getCustNum())) {
                    cateCustCount = saleMonth.getCustNum();
                } else {
                    cateCustCount = "0";
                }
                // 客单价
                cateCustAvgPrice = DJStringUtils.divide(cateTotalAmount, DJStringUtils.bigDecimal(cateCustCount));
                // 客单价同比
                if (null != saleMonth.getLastYearSaleAmount() && null != saleMonth.getLastYearCustNum()) {
                    BigDecimal cateCustAvgPriceLstYear = DJStringUtils.divide(
                            DJStringUtils.bigDecimal(saleMonth.getLastYearSaleAmount()),
                            DJStringUtils.bigDecimal(saleMonth.getLastYearCustNum()));
                    cateSaleRate = DJStringUtils.calculateYoyString(cateCustAvgPrice, cateCustAvgPriceLstYear);
                } else {
                    cateSaleRate = "0.0";
                }
                cateList.add(new BiStoreSaleCustAvgPricedCateDto(cateCode, cateName, BiSaleAmountDto
                        .valueOfLow(cateCustAvgPrice), DJStringUtils.transIntergerStr(cateCustCount), cateSaleRate,
                        BiSaleAmountDto.valueOf(cateTotalAmount)));
            }
        }

        // 根据客单价进行排序
        Collections.sort(cateList, new Comparator<BiStoreSaleCustAvgPricedCateDto>() {
            @Override
            public int compare(BiStoreSaleCustAvgPricedCateDto o1, BiStoreSaleCustAvgPricedCateDto o2) {
                BigDecimal avgPrice1 = o1.getCurrCustAvgPrice().getSortMoney();
                BigDecimal avgPrice2 = o2.getCurrCustAvgPrice().getSortMoney();
                if (null != avgPrice1 && null != avgPrice2) {
                    return avgPrice2.compareTo(avgPrice1);
                } else if (null == avgPrice2) {
                    return -1;
                } else if (null == avgPrice1) {
                    return 1;
                }
                // 降序
                return 0;
            }
        });
        dto.setCateList(cateList);
        dto.setSaleRate(saleRate);
        dto.setCustCount(DJStringUtils.transIntergerStr(custCount));
        dto.setCurrCustAvgPrice(BiSaleAmountDto.valueOfLow(currCustAvgPrice));
        dto.setTotalSaleNum(BiSaleAmountDto.valueOf(totalAmount));
        return dto;
    }
    
    /**
     * 日维度查询门店品牌平均单价
     */
    @Override
    public BiStoreSaleBrandAvgPricedDto queryBrandCommAvgPriceByDay(StoreManDto storeManDto, String cateCode,
            String date) {
        BiStoreSaleBrandAvgPricedDto dto = new BiStoreSaleBrandAvgPricedDto();
        List<BiStoreSaleBrandAvgPricedDetailsDto> brandPriceList = new ArrayList<BiStoreSaleBrandAvgPricedDetailsDto>();

        // 销售额
        BigDecimal saleAmount = BigDecimal.ZERO;
        // 销量
        String saleCnt = "0";
        // 平均单价
        BigDecimal commAvgPrice = BigDecimal.ZERO;
        // 平均单价同比
        String saleRate = "0";

        // 判断是否是查询今天的平均单价
        if (DateUtils.getDateStrByFormat(null, "yyyy-MM-dd").equals(date)) {
            // 查询品类实时数据
            Map<String, Object> cateSaleReal = biKafkaRealTimeSaleService.getStoreCateSaleDataFromRedis(
                    storeManDto.getStoreCode(), cateCode);
            if (null != cateSaleReal) {
                if (null != cateSaleReal.get("payAmt")) {
                    saleAmount = DJStringUtils.bigDecimal(String.valueOf(cateSaleReal.get("payAmt")));
                } else {
                    saleAmount = BigDecimal.ZERO;
                }
                if (null != cateSaleReal.get("saleCnt")) {
                    saleCnt = String.valueOf(cateSaleReal.get("saleCnt"));
                } else {
                    saleCnt = "0";
                }
                commAvgPrice = DJStringUtils.divide(saleAmount, DJStringUtils.bigDecimal(saleCnt));
            }
            BigDecimal saleRateDay = biKafkaRealTimeSaleService.queryStoreCateDaySaleYoy(storeManDto.getStoreCode(),
                    cateCode);
            if (null != saleRateDay) {
                saleRate = saleRateDay.multiply(new BigDecimal(100))
                        .setScale(BigDecimal.ONE.intValue(), BigDecimal.ROUND_DOWN).toString();
            } else {
                saleRate = "0.0";
            }
            // 查询品类下品牌实时数据
            List<BiBrandSale> brandList = biBrandService.getBrandSaleByDay(cateCode, ORDER_BY_AMOUNT,
                    storeManDto.getStoreCode());
            if (CollectionUtils.isNotEmpty(brandList)) {
                String brandCode;
                String brandName;
                // 品牌销售额
                BigDecimal brandSaleAmount;
                // 品牌销售数量
                String brandSaleCnt;
                // 品牌平均单价
                BigDecimal brandAvgPrice;
                // 品牌销售额同比
                String brandRate;
                for (BiBrandSale sale : brandList) {
                    brandCode = sale.getBrandCode();
                    brandName = sale.getBrandName();
                    if (null != sale.getBrandAmount()) {
                        brandSaleAmount = sale.getBrandAmount();
                    } else {
                        brandSaleAmount = BigDecimal.ZERO;
                    }
                    if (null != sale.getSaleCnt()) {
                        brandSaleCnt = String.valueOf(sale.getSaleCnt());
                    } else {
                        brandSaleCnt = "0";
                    }
                    brandAvgPrice = DJStringUtils.divide(brandSaleAmount, DJStringUtils.bigDecimal(brandSaleCnt));
                    // 计算销售额同比
                    if (null != sale.getBrandAmountLy()) {
                        brandRate = DJStringUtils.calculateYoyString(brandSaleAmount, sale.getBrandAmountLy());
                    } else {
                        brandRate = "0";
                    }
                    brandPriceList.add(new BiStoreSaleBrandAvgPricedDetailsDto(brandCode, brandName, brandRate,
                            BiSaleAmountDto.valueOfLow(brandSaleAmount), brandSaleCnt, BiSaleAmountDto
                                    .valueOfLow(brandAvgPrice)));
                }
            }

        } else {
            Map<String, Object> paramMap = new HashMap<String, Object>();
            Date time = DateUtils.getDateByStr(date, "yyyy-MM-dd");
            paramMap.put("storeCode", storeManDto.getStoreCode());
            paramMap.put("day", DateUtils.getDateStrByFormat(time, "yyyyMMdd"));
            paramMap.put("cates", cateCode);
            // 查询品类
            List<BiStoreCateSaleDataDayDto> cateSales = dalClient.queryForList(
                    "BI_STORE_DATA_WAP.QUERY_STORE_CATE_DATA_BY_DAY", paramMap, BiStoreCateSaleDataDayDto.class);
            if (CollectionUtils.isNotEmpty(cateSales)) {
                BiStoreCateSaleDataDayDto cateInfo = cateSales.get(FIRST_ELE);
                if (StringUtils.isNotBlank(cateInfo.getSaleAmount())) {
                    saleAmount = DJStringUtils.bigDecimal(cateInfo.getSaleAmount());
                } else {
                    saleAmount = BigDecimal.ZERO;
                }
                if (StringUtils.isNotBlank(cateInfo.getSaleVolume())) {
                    saleCnt = cateInfo.getSaleVolume();
                } else {
                    saleCnt = "0";
                }
                if (StringUtils.isNotBlank(cateInfo.getSaleAmount())
                        && StringUtils.isNotBlank(cateInfo.getSaleVolume())) {
                    commAvgPrice = DJStringUtils.divide(saleAmount, DJStringUtils.bigDecimal(saleCnt));
                } else {
                    commAvgPrice = BigDecimal.ZERO;
                }
                if (StringUtils.isNotBlank(cateInfo.getSaleAmount())
                        && StringUtils.isNotBlank(cateInfo.getSaleVolume())
                        && StringUtils.isNotBlank(cateInfo.getLastYearSaleAmount())
                        && StringUtils.isNotBlank(cateInfo.getLastYearVolume())) {
                    BigDecimal commAvgPriceLastYear = DJStringUtils.divide(
                            DJStringUtils.bigDecimal(cateInfo.getLastYearSaleAmount()),
                            DJStringUtils.bigDecimal(cateInfo.getLastYearVolume()));
                    saleRate = DJStringUtils.calculateYoyString(commAvgPrice, commAvgPriceLastYear);
                } else {
                    saleRate = "0";
                }
            }
            // 查询品类下品牌
            List<BiBrandSale> brandList = biBrandDaoService.queryBrandSaleByDay(cateCode, date,
                    storeManDto.getStoreCode(), ORDER_BY_AMOUNT);
            if (CollectionUtils.isNotEmpty(brandList)) {
                String brandCode;
                String brandName;
                // 品牌销售额
                BigDecimal brandSaleAmount;
                // 品牌销售数量
                String brandSaleCnt;
                // 品牌平均单价
                BigDecimal brandAvgPrice;
                // 品牌销售额同比
                String brandRate;
                for (BiBrandSale sale : brandList) {
                    brandCode = sale.getBrandCode();
                    brandName = sale.getBrandName();
                    if (null != sale.getBrandAmount()) {
                        brandSaleAmount = sale.getBrandAmount();
                    } else {
                        brandSaleAmount = BigDecimal.ZERO;
                    }
                    if (null != sale.getSaleCnt()) {
                        brandSaleCnt = String.valueOf(sale.getSaleCnt());
                    } else {
                        brandSaleCnt = "0";
                    }
                    brandAvgPrice = DJStringUtils.divide(brandSaleAmount, DJStringUtils.bigDecimal(brandSaleCnt));
                    // 计算销售额同比
                    if (null != sale.getBrandAmountLy()) {
                        brandRate = DJStringUtils.calculateYoyString(brandSaleAmount, sale.getBrandAmountLy());
                    } else {
                        brandRate = "0";
                    }
                    brandPriceList.add(new BiStoreSaleBrandAvgPricedDetailsDto(brandCode, brandName, brandRate,
                            BiSaleAmountDto.valueOfLow(brandSaleAmount), brandSaleCnt, BiSaleAmountDto
                                    .valueOfLow(brandAvgPrice)));
                }
            }

        }

        dto.setCommAvgPrice(BiSaleAmountDto.valueOfLow(commAvgPrice));
        dto.setSaleAmount(BiSaleAmountDto.valueOfLow(saleAmount));
        dto.setSaleCnt(saleCnt);
        dto.setSaleRate(saleRate);
        // 根据平均单价进行排序
        if (CollectionUtils.isNotEmpty(brandPriceList)) {
            Collections.sort(brandPriceList, new Comparator<BiStoreSaleBrandAvgPricedDetailsDto>() {
                @Override
                public int compare(BiStoreSaleBrandAvgPricedDetailsDto o1, BiStoreSaleBrandAvgPricedDetailsDto o2) {
                    String avgPrice1 = o1.getBrandAvgPrice().getAmount();
                    String avgPrice2 = o2.getBrandAvgPrice().getAmount();
                    if (null != avgPrice1 && null != avgPrice2) {
                        return DJStringUtils.bigDecimal(avgPrice1).compareTo(DJStringUtils.bigDecimal(avgPrice2));
                    } else if (null == avgPrice2) {
                        return -1;
                    } else if (null == avgPrice1) {
                        return 1;
                    }
                    // 降序
                    return 0;
                }
            });
        }
        dto.setList(brandPriceList);

        return dto;
    }

    /**
     * 日维度查询门店品类平均单价
     */
    @Override
    public BiStoreSaleCateAvgPricedDto queryCateCommAvgPriceByDay(StoreManDto storeManDto, String date) {
        BiStoreSaleCateAvgPricedDto dto = new BiStoreSaleCateAvgPricedDto();
        Map<String, Object> paramMap = new HashMap<String, Object>();
        // 初始化8个品类
        List<String> cates = new ArrayList<String>();
        cates.addAll(ConfigConstants.DEFAULT_CATE_MAP.keySet());

        // 门店销售金额(日总销售额)
        BigDecimal saleAmount = BigDecimal.ZERO;
        // 门店销售数量
        String saleCnt = "0";
        // 门店平均单价
        BigDecimal commAvgPrice = BigDecimal.ZERO;
        // 门店平均单价同比
        String saleybRate = "0.0";
        // 品类列表
        List<BiStoreSaleCateAvgPricedDetailsDto> cateList = new ArrayList<BiStoreSaleCateAvgPricedDetailsDto>();

        // 判断是否是查询今天
        if (DateUtils.getDateStrByFormat(null, "yyyy-MM-dd").equals(date)) {
            // 查询当天门店销售数据
            Map<String, Object> todaySale = biKafkaRealTimeSaleService.getStoreSaleDataFromRedis(storeManDto
                    .getStoreCode());
            if (null != todaySale) {
                if (null != todaySale.get("payAmt")) {
                    saleAmount = DJStringUtils.bigDecimal(String.valueOf(todaySale.get("payAmt")));
                }
                if (null != todaySale.get("saleCnt")) {
                    saleCnt = String.valueOf(todaySale.get("saleCnt"));
                }
            }
            commAvgPrice = DJStringUtils.divide(saleAmount, DJStringUtils.bigDecimal(saleCnt));
            // 门店去年同期销售数据
            Map<String, Object> lastYearSaleData = biKafkaRealTimeSaleService.queryLastYearStoreSaleData(storeManDto.getStoreCode());
            if (null != lastYearSaleData && null != lastYearSaleData.get("SALE_AMOUNT")
                    && null != lastYearSaleData.get("SALE_VOLUME")) {
                BigDecimal lastCommAvgPrice = DJStringUtils.divide(
                        DJStringUtils.bigDecimal(String.valueOf(lastYearSaleData.get("SALE_AMOUNT"))),
                        DJStringUtils.bigDecimal(String.valueOf(lastYearSaleData.get("SALE_VOLUME"))));
                saleybRate = DJStringUtils.calculateYoyString(commAvgPrice, lastCommAvgPrice);
            }
            
            // 八大品类
            // 查询门店品类实时销售数据
            List<Map<String, Object>> cateSales = biKafkaRealTimeSaleService.getStoreCateSaleDataFromRedis(storeManDto
                    .getStoreCode());
            if (CollectionUtils.isNotEmpty(cateSales)) {
                String cateCode;
                String cateName;
                // 品类销售额
                BigDecimal cateSaleAmount;
                // 品类销售数量
                String cateSaleCnt;
                // 品类平均价
                BigDecimal cateAvgPrice;
                // 品类平均价同比
                String cateRate;
                for (Map<String, Object> sale : cateSales) {
                    cateCode = String.valueOf(sale.get("deptCd"));
                    cateName = ConfigConstants.DEFAULT_CATE_MAP.get(cateCode);
                    // 如果品类编码,或者品类名称不存在,则不处理该品类信息
                    if (StringUtils.isBlank(cateCode) || StringUtils.isBlank(cateName)) {
                        continue;
                    }
                    if (null != sale.get("payAmt")) {
                        cateSaleAmount = DJStringUtils.bigDecimal(String.valueOf(sale.get("payAmt")));
                    } else {
                        cateSaleAmount = BigDecimal.ZERO;
                    }
                    if (null != sale.get("saleCnt")) {
                        cateSaleCnt = String.valueOf(sale.get("saleCnt"));
                    } else {
                        cateSaleCnt = "0";
                    }
                    cateAvgPrice = DJStringUtils.divide(cateSaleAmount, DJStringUtils.bigDecimal(cateSaleCnt));
                    // 计算平均单价同比
                    BigDecimal saleAmountLastYear = biKafkaRealTimeSaleService.queryLastYearStoreCateSaleAmount(storeManDto.getStoreCode(), cateCode);
                    BigDecimal saleCntLastYear =  biKafkaRealTimeSaleService.queryLastYearStoreSaleVolume(storeManDto.getStoreCode(), cateCode);
                    if (null != saleAmountLastYear && null != saleCntLastYear) {
                        BigDecimal lastYearCateAvgPrice = DJStringUtils.divide(saleAmountLastYear, saleCntLastYear);
                        cateRate = DJStringUtils.calculateYoyString(cateAvgPrice, lastYearCateAvgPrice);
                    } else {
                        cateRate = "0.0";
                    }
                    cateList.add(new BiStoreSaleCateAvgPricedDetailsDto(cateCode, cateName, BiSaleAmountDto
                            .valueOf(cateSaleAmount), DJStringUtils.transIntergerStr(cateSaleCnt), BiSaleAmountDto
                            .valueOfLow(cateAvgPrice), cateRate));
                }
            }
        } else {
            // 门店信息
            Date time = DateUtils.getDateByStr(date, "yyyy-MM-dd");
            paramMap.clear();
            paramMap.put("storeCode", storeManDto.getStoreCode());
            paramMap.put("day", DateUtils.getDateStrByFormat(time, "yyyyMMdd"));
            BiStoreSaleDataDayDto saleData = dalClient.queryForObject("BI_STORE_DATA_WAP.QUERY_STORE_DATA_BY_DAY",
                    paramMap, BiStoreSaleDataDayDto.class);
            if (null != saleData) {
                if (StringUtils.isNotBlank(saleData.getSaleAmount())) {
                    saleAmount = DJStringUtils.bigDecimal(saleData.getSaleAmount());
                }
                if (StringUtils.isNotBlank(saleData.getSaleVolume())) {
                    saleCnt = saleData.getSaleVolume();
                }
                commAvgPrice = DJStringUtils.divide(saleAmount, DJStringUtils.bigDecimal(saleCnt));
                if (StringUtils.isNotBlank(saleData.getLastYearSaleAmount())
                        && StringUtils.isNotBlank(saleData.getLastYearVolume())) {
                    BigDecimal commAvgPriceLastYear = DJStringUtils.divide(
                            DJStringUtils.bigDecimal(saleData.getLastYearSaleAmount()),
                            DJStringUtils.bigDecimal(saleData.getLastYearVolume()));
                    saleybRate = DJStringUtils.calculateYoyString(commAvgPrice, commAvgPriceLastYear);
                }

            }

            paramMap.put("storeCode", storeManDto.getStoreCode());
            paramMap.put("day", DateUtils.getDateStrByFormat(time, "yyyyMMdd"));
            paramMap.put("cates", cates);
            List<BiStoreCateSaleDataDayDto> cateSales = dalClient.queryForList(
                    "BI_STORE_DATA_WAP.QUERY_STORE_CATE_DATA_BY_DAY", paramMap, BiStoreCateSaleDataDayDto.class);
            if (CollectionUtils.isNotEmpty(cateSales)) {
                for (BiStoreCateSaleDayDto saleMonth : cateSales) {
                    String cateCode = saleMonth.getCateCode();
                    String cateName = ConfigConstants.DEFAULT_CATE_MAP.get(cateCode);
                    // 品类销售额
                    BigDecimal cateSaleAmount = BigDecimal.ZERO;
                    // 品类销售数量
                    String cateSaleCnt = "0";
                    // 品类平均价
                    BigDecimal cateAvgPrice = BigDecimal.ZERO;
                    // 品类平均价同比
                    String cateRate = "0";
                    // 如果品类编码,或者品类名称不存在,则不处理该品类信息
                    if (StringUtils.isBlank(cateCode) || StringUtils.isBlank(cateName)) {
                        continue;
                    }
                    if (StringUtils.isNotBlank(saleMonth.getSaleAmount())) {
                        cateSaleAmount = DJStringUtils.bigDecimal(saleMonth.getSaleAmount());
                    }
                    if (StringUtils.isNotBlank(saleMonth.getSaleVolume())) {
                        cateSaleCnt = saleMonth.getSaleVolume();
                    }
                    cateAvgPrice = DJStringUtils.divide(cateSaleAmount, DJStringUtils.bigDecimal(cateSaleCnt));
                    // 平均价同比
                    if (null != saleMonth.getLastYearSaleAmount() && null != saleMonth.getLastYearVolume()) {
                        BigDecimal lastYearCateAvgPrice = DJStringUtils.divide(
                                DJStringUtils.bigDecimal(saleMonth.getLastYearSaleAmount()),
                                DJStringUtils.bigDecimal(saleMonth.getLastYearVolume()));
                        cateRate = DJStringUtils.calculateYoyString(cateAvgPrice, lastYearCateAvgPrice);
                    }
                    cateList.add(new BiStoreSaleCateAvgPricedDetailsDto(cateCode, cateName, BiSaleAmountDto
                            .valueOf(cateSaleAmount), DJStringUtils.transIntergerStr(cateSaleCnt), BiSaleAmountDto
                            .valueOfLow(cateAvgPrice), cateRate));
                }
            }
        }

        dto.setCommAvgPrice(BiSaleAmountDto.valueOfLow(commAvgPrice));
        dto.setSaleAmount(BiSaleAmountDto.valueOf(saleAmount));
        dto.setSaleCnt(DJStringUtils.transIntergerStr(saleCnt));
        dto.setSaleybRate(saleybRate);
        // 根据平均单价排序
        Collections.sort(cateList, new Comparator<BiStoreSaleCateAvgPricedDetailsDto>() {
            @Override
            public int compare(BiStoreSaleCateAvgPricedDetailsDto o1, BiStoreSaleCateAvgPricedDetailsDto o2) {
                BigDecimal avgPrice1 = o1.getCateAvgPrice().getSortMoney();
                BigDecimal avgPrice2 = o2.getCateAvgPrice().getSortMoney();
                if (null != avgPrice1 && null != avgPrice2) {
                    return avgPrice2.compareTo(avgPrice1);
                } else if (null == avgPrice2) {
                    return -1;
                } else if (null == avgPrice1) {
                    return 1;
                }
                // 降序
                return 0;
            }
        });
        dto.setList(cateList);

        return dto;
    }

    /**
     * 月维度查询门店品类平均单价
     */
    @Override
    public BiStoreSaleCateAvgPricedDto queryCateCommAvgPriceByMonth(StoreManDto storeManDto, String month) {
        BiStoreSaleCateAvgPricedDto dto = new BiStoreSaleCateAvgPricedDto();
        Map<String, Object> paramMap = new HashMap<String, Object>();
        // 初始化8个品类
        List<String> cates = new ArrayList<String>();
        cates.addAll(ConfigConstants.DEFAULT_CATE_MAP.keySet());
        String storeCode = storeManDto.getStoreCode();
        // 转换时间,数据库月份格式
        String monthDb = DateUtils.getDateStrByFormat(DateUtils.getDateByStr(month, "yyyy-MM"), "yyyyMM");
        
        // 门店销售金额(日总销售额)
        BigDecimal saleAmount = BigDecimal.ZERO;
        // 门店销售数量
        String saleCnt = "0";
        // 门店平均单价
        BigDecimal commAvgPrice = BigDecimal.ZERO;
        // 门店平均单价同比
        String saleybRate = "0.0";
        // 品类列表
        List<BiStoreSaleCateAvgPricedDetailsDto> cateList = new ArrayList<BiStoreSaleCateAvgPricedDetailsDto>();
        
        // 查询门店数据
        paramMap.put("month", monthDb);
        paramMap.put("storeCode", storeCode);
        BiStoreSaleMonthDto storeMonthSale = dalClient.queryForObject("BI_STORE_DATA_WAP.QUERY_STORE_SALE_MONTH",
                paramMap, BiStoreSaleMonthDto.class);
        if (null != storeMonthSale) {
            // 总销售额
            if (StringUtils.isNotBlank(storeMonthSale.getSaleAmount())) {
                saleAmount = DJStringUtils.bigDecimal(storeMonthSale.getSaleAmount());
            }
            // 销售数量
            if (StringUtils.isNotBlank(storeMonthSale.getSaleVolume())) {
                saleCnt = storeMonthSale.getSaleVolume();
            }
            // 平均价
            commAvgPrice = DJStringUtils.divide(saleAmount, DJStringUtils.bigDecimal(saleCnt));
            if (StringUtils.isNotBlank(storeMonthSale.getLastYearSale())
                    && StringUtils.isNotBlank(storeMonthSale.getLastYearVolume())) {
                BigDecimal commAvgPriceLastYear = DJStringUtils.divide(
                        DJStringUtils.bigDecimal(storeMonthSale.getLastYearSale()),
                        DJStringUtils.bigDecimal(storeMonthSale.getLastYearVolume()));
                saleybRate = DJStringUtils.calculateYoyString(commAvgPrice, commAvgPriceLastYear);
            }
        }

        // 查询月份品类数据
        List<BiStoreCateSaleDayDto> saleMonths = dalClient.queryForList(
                "BI_STORE_DATA_WAP.QUERY_STORE_CATE_AMOUNT_RANK_MONTH", paramMap, BiStoreCateSaleDayDto.class);
        String cateCode;
        String cateName;
        // 品类销售额
        BigDecimal cateSaleAmount;
        // 品类销售数量
        String cateSaleCnt;
        // 品类平均价
        BigDecimal cateAvgPrice;
        // 品类平均单价同比
        String cateRate;
        if (CollectionUtils.isNotEmpty(saleMonths)) {
            for (BiStoreCateSaleDayDto sale : saleMonths) {
                cateCode = sale.getCateCode();
                cateName = ConfigConstants.DEFAULT_CATE_MAP.get(cateCode);
                // 如果品类编码,或者品类名称不存在,则不处理该品类信息
                if (StringUtils.isBlank(cateCode) || StringUtils.isBlank(cateName)) {
                    continue;
                }
                if (StringUtils.isNotBlank(sale.getSaleAmount())) {
                    cateSaleAmount = DJStringUtils.bigDecimal(sale.getSaleAmount());
                } else {
                    cateSaleAmount = BigDecimal.ZERO;
                }
                if (StringUtils.isNotBlank(sale.getSaleVolume())) {
                    cateSaleCnt = String.valueOf(sale.getSaleVolume());
                } else {
                    cateSaleCnt = "0";
                }
                cateAvgPrice = DJStringUtils.divide(cateSaleAmount, DJStringUtils.bigDecimal(cateSaleCnt));
                // 计算平均单价同比(总销售额/销售数量)
                if (StringUtils.isNotBlank(sale.getLastYearSaleAmount()) && StringUtils.isNotBlank(sale.getLastYearVolume())) {
                    BigDecimal lastYearCateAvgPrice = DJStringUtils.divide(
                            DJStringUtils.bigDecimal(sale.getLastYearSaleAmount()),
                            DJStringUtils.bigDecimal(sale.getLastYearVolume()));
                    cateRate = DJStringUtils.calculateYoyString(cateAvgPrice, lastYearCateAvgPrice);
                } else {
                    cateRate = "0.0";
                }
                cateList.add(new BiStoreSaleCateAvgPricedDetailsDto(cateCode, cateName, BiSaleAmountDto
                        .valueOf(cateSaleAmount), DJStringUtils.transIntergerStr(cateSaleCnt), BiSaleAmountDto.valueOfLow(cateAvgPrice),
                        cateRate));
            }
        }
        
        dto.setCommAvgPrice(BiSaleAmountDto.valueOfLow(commAvgPrice));
        dto.setSaleAmount(BiSaleAmountDto.valueOf(saleAmount));
        dto.setSaleCnt(DJStringUtils.transIntergerStr(saleCnt));
        dto.setSaleybRate(saleybRate);
        // 根据平均单价进行排序
        if (CollectionUtils.isNotEmpty(cateList)) {
            Collections.sort(cateList, new Comparator<BiStoreSaleCateAvgPricedDetailsDto>() {
                @Override
                public int compare(BiStoreSaleCateAvgPricedDetailsDto o1, BiStoreSaleCateAvgPricedDetailsDto o2) {
                    BigDecimal avgPrice1 = o1.getCateAvgPrice().getSortMoney();
                    BigDecimal avgPrice2 = o2.getCateAvgPrice().getSortMoney();
                    if (null != avgPrice1 && null != avgPrice2) {
                        return avgPrice2.compareTo(avgPrice1);
                    } else if (null == avgPrice2) {
                        return -1;
                    } else if (null == avgPrice1) {
                        return 1;
                    }
                    // 降序
                    return 0;
                }
            });
        }
        dto.setList(cateList);
        
        return dto;
    }
	 /**
     * 查询日维度延保品类列表页面信息
     */
    @Override
    public BiStoreYbDetailDto queryYbCateByDay(String storeCode, String day) {
        LOGGER.info("enter queryYbCateByDay, storeCode={},day={}", storeCode, day);
        
        // 判断是否是查询今天的销售排行
        if (DateUtils.getDateStrByFormat(null, "yyyy-MM-dd").equals(day)) {
            // 查询门店总销售额
            BigDecimal totalAmount;
            Map<String, Object> todaySale = biKafkaRealTimeSaleService.getStoreSaleDataFromRedis(storeCode);
            if (null == todaySale || null == todaySale.get("payAmt")) {
                totalAmount = BigDecimal.ZERO;
            } else {
                totalAmount = DJStringUtils.bigDecimal(String.valueOf(todaySale.get("payAmt")));
            }
            //当日实时延保门店销售额
            BigDecimal ybAmount = biKafkaRealTimeSaleService.getStoreYbSaleFromRedis(storeCode);
            BiStoreYbDetailDto biStoreYbDetailDto = new BiStoreYbDetailDto();
            biStoreYbDetailDto.setYbSaleAmount(BiSaleAmountDto.valueOf(ybAmount));
            biStoreYbDetailDto.setYbRate(DJStringUtils.calculateRateString(ybAmount.toString(), totalAmount.toString()));
            
            // 当日延保品类实时销售数据
            List<Map<String, Object>> cateSales = biKafkaRealTimeSaleService.getStoreYbCateDataFromRedis(storeCode);
            List<BiStoreYbCateDetailDto> cateSaleList = new ArrayList<BiStoreYbCateDetailDto>();
            // 数据不为空时
            if (CollectionUtils.isEmpty(cateSales)) {
                biStoreYbDetailDto.setList(cateSaleList);
                return biStoreYbDetailDto;
            }
            // 根据销售额排序
            Collections.sort(cateSales, new Comparator<Map<String, Object>>() {
                @Override
                public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                    if (null != o2.get("payAmt") && null != o1.get("payAmt")) {
                        return DJStringUtils.bigDecimal(String.valueOf(o2.get("payAmt"))).compareTo(DJStringUtils.bigDecimal(String.valueOf(o1.get("payAmt"))));
                    } else if (null == o2.get("payAmt")) {
                        return -1;
                    } else if (null == o1.get("payAmt")) {
                        return 1;
                    }
                    // 降序
                    return 0;
                }
            });
            //设置品类详情
            for (Map<String, Object> sale : cateSales) {
                // 获取品类编码
                String cateCode = ConfigConstants.DEFAULT_YB_CATE_MAP.get(String.valueOf(sale.get("l4GdsGroupCd")));
                String cateName = ConfigConstants.DEFAULT_CATE_MAP.get(cateCode);
                // 判断品类编码和品类名称是否为空
                if (StringUtils.isBlank(cateCode) || StringUtils.isBlank(cateName)) {
                    // 如果品类编码,或者品类名称不存在,则不处理该品类信息
                    continue;
                }
                BiStoreYbCateDetailDto cateDetail;
                cateDetail = new BiStoreYbCateDetailDto();
                cateDetail.setCateCode(cateCode);
                cateDetail.setCateName(cateName);
                BigDecimal cateYbSaleAmount;
                if (null != sale.get("payAmt")) {
                    cateYbSaleAmount = DJStringUtils.bigDecimal(String.valueOf(sale.get("payAmt")));
                } else {
                    cateYbSaleAmount = BigDecimal.ZERO;
                }
                //延保品类销售额
                cateDetail.setCateYbSaleAmount(BiSaleAmountDto.valueOf(cateYbSaleAmount));
                //如果都是0则为无意义数据
                if(BigDecimal.ZERO.compareTo(cateYbSaleAmount) == 0){
                    continue;
                }
                //延保品类占比
                BigDecimal cateSaleAmount =  biKafkaRealTimeSaleService.queryStoreCateDaySale(storeCode, cateCode);
                if(cateYbSaleAmount != null &&cateSaleAmount != null ){
                    cateDetail.setCateYbRate(DJStringUtils.calculateRateString(cateYbSaleAmount.toString(), cateSaleAmount.toString()));
                }else{
                    cateDetail.setCateYbRate("0.0");
                }
                
                cateSaleList.add(cateDetail);
            }
            biStoreYbDetailDto.setList(cateSaleList);
            return biStoreYbDetailDto;
        } else {
            // 查询门店总销售额
            BigDecimal totalAmount;
            //门店延保日销售额
            BigDecimal ybAmount;
            Date time = DateUtils.getDateByStr(day, "yyyy-MM-dd");
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("storeCode", storeCode);
            // 设置有效天数
            paramMap.put("day", DateUtils.getDateStrByFormat(time, "yyyyMMdd"));
            
            
            // 查询日延保销售数据
            BiStoreYbDayDto ybSaleTotal = dalClient.queryForObject("BI_STORE_YB.QUERY_STORE_YB_DAY",
                    paramMap, BiStoreYbDayDto.class);
            if (null == ybSaleTotal || StringUtils.isBlank(ybSaleTotal.getYbSaleAmount())) {
                ybAmount = BigDecimal.ZERO;
            } else {
                ybAmount = DJStringUtils.bigDecimal(ybSaleTotal.getYbSaleAmount());
            }
            //日门店总销售
            if (null == ybSaleTotal || StringUtils.isBlank(ybSaleTotal.getSaleAmount())) {
                totalAmount = BigDecimal.ZERO;
            } else {
                totalAmount = DJStringUtils.bigDecimal(ybSaleTotal.getSaleAmount());
            }
            
            BiStoreYbDetailDto biStoreYbDetailDto = new BiStoreYbDetailDto();
            biStoreYbDetailDto.setYbSaleAmount(BiSaleAmountDto.valueOf(ybAmount));
            biStoreYbDetailDto.setYbRate(DJStringUtils.calculateRateString(ybAmount.toString(), totalAmount.toString()));
            
            // 查询延保品类销售详情列表
            List<BiStoreYbDayDto> ybSaleCateList = dalClient.queryForList(
                    "BI_STORE_YB.QUERY_CATE_LIST_YB_DAY", paramMap, BiStoreYbDayDto.class);
            LOGGER.debug("queryYbCateByDay ybSaleCateList={}", ybSaleCateList);
            List<BiStoreYbCateDetailDto> cateSaleList = new ArrayList<BiStoreYbCateDetailDto>();
            // 数据为空时
            if (CollectionUtils.isEmpty(ybSaleCateList)) {
                biStoreYbDetailDto.setList(cateSaleList);
                return biStoreYbDetailDto;
            }
            BiStoreYbCateDetailDto ybCateDetail;
            //延保销售额
            for (BiStoreYbDayDto saleDay : ybSaleCateList) {
                // 获取品类编码
                String cateCode = saleDay.getCateCode();
                String cateName = ConfigConstants.DEFAULT_CATE_MAP.get(cateCode);
                // 判断品类编码和品类名称是否为空
                if (StringUtils.isBlank(cateCode) || StringUtils.isBlank(cateName)) {
                    // 如果品类编码,或者品类名称不存在,则不处理该品类信息
                    continue;
                }
                ybCateDetail = new BiStoreYbCateDetailDto();
                ybCateDetail.setCateCode(cateCode);
                ybCateDetail.setCateName(cateName);
                BigDecimal ybCateAmount;
                BigDecimal cateAmount;
                if (StringUtils.isNotBlank(saleDay.getSaleAmount())) {
                    cateAmount = DJStringUtils.bigDecimal(saleDay.getSaleAmount());
                } else {
                    cateAmount = BigDecimal.ZERO;
                }
                
                if (StringUtils.isNotBlank(saleDay.getYbSaleAmount())) {
                    ybCateAmount = DJStringUtils.bigDecimal(saleDay.getYbSaleAmount());
                } else {
                    ybCateAmount = BigDecimal.ZERO;
                }
                //如果都是0则为无意义数据
                if(BigDecimal.ZERO.compareTo(ybCateAmount) == 0){
                    continue;
                }
                // 日延保品类销售额
                ybCateDetail.setCateYbSaleAmount(BiSaleAmountDto.valueOf(ybCateAmount));
                //日延保品类销售占比
                if(ybCateAmount != null &&cateAmount != null ){
                    ybCateDetail.setCateYbRate(DJStringUtils.calculateRateString(ybCateAmount.toString(), cateAmount.toString()));
                }else{
                    ybCateDetail.setCateYbRate("0.0");
                }
                cateSaleList.add(ybCateDetail);
            }
            biStoreYbDetailDto.setList(cateSaleList);
            return biStoreYbDetailDto;
        }
    }

    @Override
    public BiStoreYbDetailDto queryYbCateByMonth(String storeCode, String month) {

        LOGGER.info("enter queryYbCateByMonth, storeCode={},month={}", storeCode, month);
        
        // 判断是否是查询本月的销售排行
        if (DateUtils.getDateStrByFormat(null, "yyyy-MM").equals(month)) {
            // 查询门店当日实时总销售额
            BigDecimal totalAmount;
            Map<String, Object> todaySale = biKafkaRealTimeSaleService.getStoreSaleDataFromRedis(storeCode);
            if (null == todaySale || null == todaySale.get("payAmt")) {
                totalAmount = BigDecimal.ZERO;
            } else {
                totalAmount = DJStringUtils.bigDecimal(String.valueOf(todaySale.get("payAmt")));
            }
            //当日实时延保门店销售额
            BigDecimal ybAmount = biKafkaRealTimeSaleService.getStoreYbSaleFromRedis(storeCode);
            
            Date time = DateUtils.getDateByStr(month, "yyyy-MM");
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("storeCode", storeCode);
            // 设置有效天数
            paramMap.put("month", DateUtils.getDateStrByFormat(time, "yyyyMM"));
            
            // 查询月延保销售数据
            BiStoreYbMonthDto saleTotal = dalClient.queryForObject("BI_STORE_YB.QUERY_STORE_YB_MONTH",
                    paramMap, BiStoreYbMonthDto.class);
            // 当月延保销售额
            if (null != saleTotal && StringUtils.isNotBlank(saleTotal.getYbSaleAmount())) {
                ybAmount = ybAmount.add(DJStringUtils.bigDecimal(saleTotal.getYbSaleAmount()));
            }
            //当月总销售额
            if (null != saleTotal && StringUtils.isNotBlank(saleTotal.getSaleAmount())) {
                totalAmount = totalAmount.add(DJStringUtils.bigDecimal(saleTotal.getSaleAmount()));
            }
            
            BiStoreYbDetailDto biStoreYbDetailDto = new BiStoreYbDetailDto();
            biStoreYbDetailDto.setYbSaleAmount(BiSaleAmountDto.valueOf(ybAmount));
            biStoreYbDetailDto.setYbRate(DJStringUtils.calculateRateString(ybAmount.toString(), totalAmount.toString()));
            
            //查询当月往期延保品类销售列表
            List<BiStoreYbMonthDto> ybCateList = null;
            // 查询月延保销售数据
            try {
                ybCateList = dalClient.queryForList("BI_STORE_YB.QUERY_CATE_LIST_YB_MONTH",
                        paramMap, BiStoreYbMonthDto.class);
            } catch (Exception e) {
                LOGGER.error("QUERY_CATE_LIST_YB_MONTH error storeCode:"+ storeCode + ",month" + month ,e );
            }
            
            @SuppressWarnings("serial")
            List<String> cateList = new ArrayList<String>(){ { 
                    add("00001");  
                    add("00002");  
                    add("00003");  
                    add("00004");  
                    add("00005");  
                    add("00006");  
                    add("00007");  
                    add("00013");  
                }
            };
            
            List<BiStoreYbCateDetailDto> cateSaleList = new ArrayList<BiStoreYbCateDetailDto>();
            
            //设置品类详情
            if (CollectionUtils.isNotEmpty(ybCateList)) {
                for (BiStoreYbMonthDto item : ybCateList) {
                    if(item == null ){
                        continue;
                    }
                    // 获取品类编码
                    String cateCode = item.getCateCode();
                    String cateName = ConfigConstants.DEFAULT_CATE_MAP.get(cateCode);
                    // 判断品类编码和品类名称是否为空
                    if (StringUtils.isBlank(cateCode) || StringUtils.isBlank(cateName)) {
                        // 如果品类编码,或者品类名称不存在,则不处理该品类信息
                        continue;
                    }
                    BiStoreYbCateDetailDto cateDetail = new BiStoreYbCateDetailDto();
                    cateDetail.setCateCode(cateCode);
                    cateDetail.setCateName(cateName);
                    
                    // 本月离线延保品类销售额
                    BigDecimal cateYbSaleAmount  = DJStringUtils.bigDecimal(item.getYbSaleAmount());
                    if (null == cateYbSaleAmount) {
                        cateYbSaleAmount = BigDecimal.ZERO;
                    }
                    BigDecimal ybcateCurrAmount = biKafkaRealTimeSaleService.getStoreYbCateSaleFromRedis(storeCode, cateCode);
                    //本月离线延保品类销售额 加上 当日实时延保品类销售额为本月整体销售额
                    cateYbSaleAmount = cateYbSaleAmount.add(ybcateCurrAmount);
                    //如果都是0则为无意义数据
                    if(BigDecimal.ZERO.compareTo(cateYbSaleAmount) == 0){
                        continue;
                    }
                    //延保品类销售额
                    cateDetail.setCateYbSaleAmount(BiSaleAmountDto.valueOf(cateYbSaleAmount));
                    cateDetail.setOrderParm(cateYbSaleAmount.toString());
                    //延保品类占比
                    //本月离线品类销售额
                    BigDecimal cateSaleAmount  = DJStringUtils.bigDecimal(item.getSaleAmount());
                    //当日实时品类销售额
                    BigDecimal cateSaleCurrAmount =  biKafkaRealTimeSaleService.queryStoreCateDaySale(storeCode, cateCode);
                    cateSaleAmount = cateSaleAmount.add(cateSaleCurrAmount);
                    if(cateYbSaleAmount != null &&cateSaleAmount != null ){
                        cateDetail.setCateYbRate(DJStringUtils.calculateRateString(cateYbSaleAmount.toString(), cateSaleAmount.toString()));
                    }else{
                        cateDetail.setCateYbRate("0.0");
                    }
                    
                    if(cateList.contains(cateCode)){
                        cateList.remove(cateCode);
                    }
                    cateSaleList.add(cateDetail);
                }
            }

            if(CollectionUtils.isNotEmpty(cateList)){
                for(String cateCode : cateList){
                    // 获取品类编码
                    String cateName = ConfigConstants.DEFAULT_CATE_MAP.get(cateCode);
                    // 判断品类编码和品类名称是否为空
                    if (StringUtils.isBlank(cateCode) || StringUtils.isBlank(cateName)) {
                        // 如果品类编码,或者品类名称不存在,则不处理该品类信息
                        continue;
                    }
                    BiStoreYbCateDetailDto cateDetail = new BiStoreYbCateDetailDto();
                    cateDetail.setCateCode(cateCode);
                    cateDetail.setCateName(cateName);
                    
                    // 当日实时延保品类销售额
                    BigDecimal ybcateCurrAmount = biKafkaRealTimeSaleService.getStoreYbCateSaleFromRedis(storeCode, cateCode);
                    //延保品类销售额
                    cateDetail.setCateYbSaleAmount(BiSaleAmountDto.valueOf(ybcateCurrAmount));
                    cateDetail.setOrderParm(ybcateCurrAmount.toString());
                    //延保品类占比
                    //当日实时品类销售额
                    BigDecimal cateSaleCurrAmount =  biKafkaRealTimeSaleService.queryStoreCateDaySale(storeCode, cateCode);
                    if(cateSaleCurrAmount != null &&ybcateCurrAmount != null ){
                        cateDetail.setCateYbRate(DJStringUtils.calculateRateString(ybcateCurrAmount.toString(), cateSaleCurrAmount.toString()));
                    }else{
                        cateDetail.setCateYbRate("0.0");
                    }
                    
                    //如果都是0则为无意义数据
                    if(BigDecimal.ZERO.compareTo(ybcateCurrAmount) == 0){
                        continue;
                    }
                    cateSaleList.add(cateDetail);
                }
            }
            
            // 根据销售额排序
            Collections.sort(cateSaleList, new Comparator<BiStoreYbCateDetailDto>() {
                @Override
                public int compare(BiStoreYbCateDetailDto o1, BiStoreYbCateDetailDto o2) {
                    if (o2 !=null && StringUtils.isNotBlank(o2.getOrderParm()) && o1 !=null && StringUtils.isNotBlank(o1.getOrderParm()) ) {
                        return DJStringUtils.bigDecimal(o2.getOrderParm()).compareTo(DJStringUtils.bigDecimal(o1.getOrderParm()));
                    } else if (o2 ==null || StringUtils.isBlank(o2.getOrderParm())) {
                        return -1;
                    } else if (o1 == null || StringUtils.isBlank(o1.getOrderParm())) {
                        return 1;
                    }
                    // 降序
                    return 0;
                }
            });
            
            biStoreYbDetailDto.setList(cateSaleList);
            return biStoreYbDetailDto;
        } else {
            //非本月
            // 查询月延保销售数据
            Date time = DateUtils.getDateByStr(month, "yyyy-MM");
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("storeCode", storeCode);
            paramMap.put("month", DateUtils.getDateStrByFormat(time, "yyyyMM"));
            BiStoreYbMonthDto saleTotal = null;
            try {
                saleTotal = dalClient.queryForObject("BI_STORE_YB.QUERY_STORE_YB_MONTH",
                        paramMap, BiStoreYbMonthDto.class);
            } catch (Exception e) {
                LOGGER.error("QUERY_STORE_YB_MONTH error storeCode:"+ storeCode + ",month" + month ,e );
            }
            
            // 当月延保销售额
            BigDecimal ybAmount;
            if (null != saleTotal && StringUtils.isNotBlank(saleTotal.getYbSaleAmount())) {
                ybAmount = DJStringUtils.bigDecimal(saleTotal.getYbSaleAmount());
            }else{
                ybAmount = BigDecimal.ZERO;
            }
            //当月总销售额
            BigDecimal totalAmount;
            if (null != saleTotal && StringUtils.isNotBlank(saleTotal.getSaleAmount())) {
                totalAmount = DJStringUtils.bigDecimal(saleTotal.getSaleAmount());
            }else{
                totalAmount = BigDecimal.ZERO;
            }
            
            BiStoreYbDetailDto biStoreYbDetailDto = new BiStoreYbDetailDto();
            biStoreYbDetailDto.setYbSaleAmount(BiSaleAmountDto.valueOf(ybAmount));
            biStoreYbDetailDto.setYbRate(DJStringUtils.calculateRateString(ybAmount.toString(), totalAmount.toString()));
            
            //查询月延保品类销售列表
            List<BiStoreYbMonthDto> ybCateList = null;
            try {
                 ybCateList = dalClient.queryForList("BI_STORE_YB.QUERY_CATE_LIST_YB_MONTH",
                        paramMap, BiStoreYbMonthDto.class);
            } catch (Exception e) {
                LOGGER.error("QUERY_CATE_LIST_YB_MONTH error storeCode:"+ storeCode + ",month" + month ,e );
            }
            
            List<BiStoreYbCateDetailDto> cateSaleList = new ArrayList<BiStoreYbCateDetailDto>();
            // 数据不为空时
              if (CollectionUtils.isEmpty(ybCateList)) {
                  biStoreYbDetailDto.setList(cateSaleList);
                  return biStoreYbDetailDto;
              }
            //设置品类详情
            for (BiStoreYbMonthDto item : ybCateList) {
                if(item == null ){
                    continue;
                }
                // 获取品类编码
                String cateCode = item.getCateCode();
                String cateName = ConfigConstants.DEFAULT_CATE_MAP.get(cateCode);
                // 判断品类编码和品类名称是否为空
                if (StringUtils.isBlank(cateCode) || StringUtils.isBlank(cateName)) {
                    // 如果品类编码,或者品类名称不存在,则不处理该品类信息
                    continue;
                }
                BiStoreYbCateDetailDto cateDetail = new BiStoreYbCateDetailDto();
                cateDetail.setCateCode(cateCode);
                cateDetail.setCateName(cateName);
                
                // 月离线延保品类销售额
                BigDecimal cateYbSaleAmount  = DJStringUtils.bigDecimal(item.getYbSaleAmount());
                //如果延保销售为空则忽略
                if(BigDecimal.ZERO.compareTo(cateYbSaleAmount) == 0){
                    continue;
                }
                cateDetail.setCateYbSaleAmount(BiSaleAmountDto.valueOf(cateYbSaleAmount));
                
                //延保品类占比
                //本月离线品类销售额
                BigDecimal cateSaleAmount  = DJStringUtils.bigDecimal(item.getSaleAmount());
                if(cateYbSaleAmount != null &&cateSaleAmount != null ){
                    cateDetail.setCateYbRate(DJStringUtils.calculateRateString(cateYbSaleAmount.toString(), cateSaleAmount.toString()));
                }else{
                    cateDetail.setCateYbRate("0.0");
                }
                cateSaleList.add(cateDetail);
            }
            
            biStoreYbDetailDto.setList(cateSaleList);
            return biStoreYbDetailDto;
        }
    }

    @Override
    public BiStoreYbCateInfo queryYbStaffByDay(StoreManDto storeManDto, String day, String cateCode) {
        LOGGER.info("enter queryYbStaffByDay, storeCode={},day={},cateCode={}", storeManDto.getStoreCode(), day,cateCode);

        // 判断是否是查询今天
        if (DateUtils.getDateStrByFormat(null, "yyyy-MM-dd").equals(day)) {
            // 查询品类总销售额
            BigDecimal totalAmount;
            Map<String, Object> todaySale = biKafkaRealTimeSaleService.getStoreCateSaleDataFromRedis(storeManDto.getStoreCode(), cateCode);
            if (null == todaySale || null == todaySale.get("payAmt")) {
                totalAmount = BigDecimal.ZERO;
            } else {
                totalAmount = DJStringUtils.bigDecimal(String.valueOf(todaySale.get("payAmt")));
            }
            //当日实时延保品类销售额
            BigDecimal ybAmount = biKafkaRealTimeSaleService.getStoreYbCateSaleFromRedis(storeManDto.getStoreCode(), cateCode);
            BiStoreYbCateInfo biStoreYbCateInfo = new BiStoreYbCateInfo();
            biStoreYbCateInfo.setYbSaleAmount(BiSaleAmountDto.valueOf(ybAmount));
            biStoreYbCateInfo.setYbRate(DJStringUtils.calculateRateString(ybAmount.toString(), totalAmount.toString()));
            
            List<SaleCategory> cateList = storeGuideBiService.getBlongGategoriesAll(storeManDto, cateCode);
            biStoreYbCateInfo.setCateList(cateList);
            
            // 当日延保品类实时销售数据
            List<Map<String, Object>> satffYbCurrSales = biKafkaRealTimeSaleService.getStoreYbStaffDataFromRedis(storeManDto.getStoreCode(),cateCode);
            List<BiStoreYbStaffDetailDto> straffYbSaleList = new ArrayList<BiStoreYbStaffDetailDto>();
            // 数据不为空时
            if (CollectionUtils.isEmpty(satffYbCurrSales)) {
                biStoreYbCateInfo.setCateList(cateList);
                return biStoreYbCateInfo;
            }
            // 根据销售额排序
            Collections.sort(satffYbCurrSales, new Comparator<Map<String, Object>>() {
                @Override
                public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                    if (null != o2.get("payAmt") && null != o1.get("payAmt")) {
                        return DJStringUtils.bigDecimal(String.valueOf(o2.get("payAmt"))).compareTo(DJStringUtils.bigDecimal(String.valueOf(o1.get("payAmt"))));
                    } else if (null == o2.get("payAmt")) {
                        return -1;
                    } else if (null == o1.get("payAmt")) {
                        return 1;
                    }
                    // 降序
                    return 0;
                }
            });
            //设置人员详情
            for (Map<String, Object> item : satffYbCurrSales) {
                BiStoreYbStaffDetailDto staffDetail = new BiStoreYbStaffDetailDto();
                if (null != item.get("salesPerson")) {
                    staffDetail.setStaffId(String.valueOf(item.get("salesPerson")));
                } else{
                    continue;
                }
                Employee employee = employeeService.queryEmployeeBasicInfo(String.valueOf(item.get("salesPerson")));
                if(employee != null && StringUtils.isNotBlank(employee.getEmployeeName())){
                    staffDetail.setStaffName(employee.getEmployeeName());
                }else{
                    staffDetail.setStaffName(String.valueOf(item.get("salesPerson")));
                }
                BigDecimal staffYbSaleAmount;
                if (null != item.get("payAmt")) {
                    staffYbSaleAmount = DJStringUtils.bigDecimal(String.valueOf(item.get("payAmt")));
                } else {
                    staffYbSaleAmount = BigDecimal.ZERO;
                }
                //延保品类销售额
                //如果延保销售为空则忽略
                if(BigDecimal.ZERO.compareTo(staffYbSaleAmount) == 0){
                    continue;
                }
                staffDetail.setStaffYbSaleAmount(BiSaleAmountDto.valueOf(staffYbSaleAmount,""));
                
                straffYbSaleList.add(staffDetail);
            }
            biStoreYbCateInfo.setList(straffYbSaleList);
            
            return biStoreYbCateInfo;
        } else {
            // 查询日延保销售数据
            Date time = DateUtils.getDateByStr(day, "yyyy-MM-dd");
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("storeCode", storeManDto.getStoreCode());
            paramMap.put("day", DateUtils.getDateStrByFormat(time, "yyyyMMdd"));
            paramMap.put("cateCode", cateCode);
            BiStoreYbDayDto ybCateSaleTotal = null;
            try {
                ybCateSaleTotal = dalClient.queryForObject("BI_STORE_YB.QUERY_CATE_YB_DAY",
                        paramMap, BiStoreYbDayDto.class);
            } catch (Exception e2) {
                LOGGER.error("QUERY_CATE_YB_DAY error storeCode:"+ storeManDto.getStoreCode() + ",day" + day+ ",cateCode" + cateCode ,e2 );
            }
            
            //门店品类延保日销售额
            BigDecimal ybCateAmount;
            if (null == ybCateSaleTotal || StringUtils.isBlank(ybCateSaleTotal.getYbSaleAmount())) {
                ybCateAmount = BigDecimal.ZERO;
            } else {
                ybCateAmount = DJStringUtils.bigDecimal(ybCateSaleTotal.getYbSaleAmount());
            }
            // 查询品类销售额
            BigDecimal cateAmount;
            if (null == ybCateSaleTotal || StringUtils.isBlank(ybCateSaleTotal.getSaleAmount())) {
                cateAmount = BigDecimal.ZERO;
            } else {
                cateAmount = DJStringUtils.bigDecimal(ybCateSaleTotal.getSaleAmount());
            }
            
            BiStoreYbCateInfo biStoreYbCateInfo = new BiStoreYbCateInfo();
            biStoreYbCateInfo.setYbSaleAmount(BiSaleAmountDto.valueOf(ybCateAmount));
            biStoreYbCateInfo.setYbRate(DJStringUtils.calculateRateString(ybCateAmount.toString(), cateAmount.toString()));
            List<SaleCategory> cateList = storeGuideBiService.getBlongGategoriesAll(storeManDto, cateCode);
            biStoreYbCateInfo.setCateList(cateList);
            
            // 查询延保品类销售详情列表
            List<BiStoreYbDayDto> ybSaleStaffList = null;
            try {
                ybSaleStaffList = dalClient.queryForList(
                        "BI_STORE_YB.QUERY_STAFF_LIST_YB_DAY", paramMap, BiStoreYbDayDto.class);
            } catch (Exception e) {
                LOGGER.error("QUERY_STAFF_LIST_YB_DAY error storeCode:"+ storeManDto.getStoreCode() + ",day" + day+ ",cateCode" + cateCode ,e );
            }
            LOGGER.debug("queryYbStaffByDay ybSaleStaffList={}", ybSaleStaffList);
            //延保销售额
            List<BiStoreYbStaffDetailDto> cateSaleList = new ArrayList<BiStoreYbStaffDetailDto>();
            // 数据为空时
            if (CollectionUtils.isEmpty(ybSaleStaffList)) {
                biStoreYbCateInfo.setList(cateSaleList);
                return biStoreYbCateInfo;
            }
            
            for (BiStoreYbDayDto saleDay : ybSaleStaffList) {
                BiStoreYbStaffDetailDto  ybCateDetail = new BiStoreYbStaffDetailDto();
                if(saleDay == null){
                    continue;
                }
                if (StringUtils.isNotBlank(saleDay.getStaffId())) {
                    ybCateDetail.setStaffId(saleDay.getStaffId());
                } else{
                    continue;
                }
                
                ybCateDetail.setStaffName(getStaffNameForYB(saleDay.getStaffId()));
                

                //员工延保品类销售额
                BigDecimal ybStaffAmount;
                if (StringUtils.isNotBlank(saleDay.getYbSaleAmount())) {
                    ybStaffAmount = DJStringUtils.bigDecimal(saleDay.getYbSaleAmount());
                } else {
                    ybStaffAmount = BigDecimal.ZERO;
                }
                //如果延保销售为空则忽略
                if(BigDecimal.ZERO.compareTo(ybStaffAmount) == 0){
                    continue;
                }
                // 日延保品类销售额
                ybCateDetail.setStaffYbSaleAmount(BiSaleAmountDto.valueOf(ybStaffAmount,""));
                cateSaleList.add(ybCateDetail);
            }
            biStoreYbCateInfo.setList(cateSaleList);
            return biStoreYbCateInfo;
        }
    }

    
    
    private String getStaffNameForYB(String staffId) {
        String staffName = staffId;;
        if (DJYB.equals(staffId)) {
            staffName = DJYB_NAME;
        } else {
            Employee employee = employeeService.queryEmployeeBasicInfo(staffId);
            if (employee != null && StringUtils.isNotBlank(employee.getEmployeeName())) {
                staffName = employee.getEmployeeName();
            } else {
                staffName = staffId;
            }
        }
        return staffName;
    }

    @Override
    public BiStoreYbCateInfo queryYbStaffByMonth(StoreManDto storeManDto, String month, String cateCode) {

        LOGGER.info("enter queryYbStaffByMonth, storeCode={},month={},cateCode={}", storeManDto.getStoreCode(), month,cateCode);

        // 判断是否是查询当月
        if (DateUtils.getDateStrByFormat(null, "yyyy-MM").equals(month)) {
            // 查询品类总销售额
            BigDecimal totalAmount;
            //当月品类实时总销售额
            Map<String, Object> todaySale = biKafkaRealTimeSaleService.getStoreCateSaleDataFromRedis(storeManDto.getStoreCode(), cateCode);
            if (null == todaySale || null == todaySale.get("payAmt")) {
                totalAmount = BigDecimal.ZERO;
            } else {
                totalAmount = DJStringUtils.bigDecimal(String.valueOf(todaySale.get("payAmt")));
            }
            
            // 当月延保品类销售数据
            Date time = DateUtils.getDateByStr(month, "yyyy-MM");
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("storeCode", storeManDto.getStoreCode());
            paramMap.put("month", DateUtils.getDateStrByFormat(time, "yyyyMM"));
            paramMap.put("cateCode", cateCode);
            BiStoreYbDayDto ybCateSaleTotal = null;
            try {
                ybCateSaleTotal = dalClient.queryForObject("BI_STORE_YB.QUERY_CATE_YB_MONTH",
                        paramMap, BiStoreYbDayDto.class);
            } catch (Exception e2) {
                LOGGER.error("QUERY_STAFF_LIST_YB_DAY error storeCode:"+ storeManDto.getStoreCode() + ",month" + month+ ",cateCode" + cateCode ,e2);
            }
            // 当月品类销售额
            BigDecimal cateAmount;
            if (null == ybCateSaleTotal || StringUtils.isBlank(ybCateSaleTotal.getSaleAmount())) {
                cateAmount = BigDecimal.ZERO;
            } else {
                cateAmount = DJStringUtils.bigDecimal(ybCateSaleTotal.getSaleAmount());
            }
            totalAmount = totalAmount.add(cateAmount);
            
            //当日实时延保品类销售额
            BigDecimal ybAmount = biKafkaRealTimeSaleService.getStoreYbCateSaleFromRedis(storeManDto.getStoreCode(), cateCode);
            
            //当月品类延保销售额
            BigDecimal ybCateAmount;
            if (null == ybCateSaleTotal || StringUtils.isBlank(ybCateSaleTotal.getYbSaleAmount())) {
                ybCateAmount = BigDecimal.ZERO;
            } else {
                ybCateAmount = DJStringUtils.bigDecimal(ybCateSaleTotal.getYbSaleAmount());
            }
            ybAmount = ybAmount.add(ybCateAmount);
            
            BiStoreYbCateInfo biStoreYbCateInfo = new BiStoreYbCateInfo();
            biStoreYbCateInfo.setYbSaleAmount(BiSaleAmountDto.valueOf(ybAmount));
            biStoreYbCateInfo.setYbRate(DJStringUtils.calculateRateString(ybAmount.toString(), totalAmount.toString()));
            List<SaleCategory> cateList = storeGuideBiService.getBlongGategoriesAll(storeManDto, cateCode);
            biStoreYbCateInfo.setCateList(cateList);
            // 当月延保品类员工销售列表
            List<BiStoreYbMonthDto> ybMonthStaffList = null;
            try {
              ybMonthStaffList = dalClient.queryForList("BI_STORE_YB.QUERY_STAFF_LIST_YB_MONTH",
                        paramMap, BiStoreYbMonthDto.class);
            } catch (Exception e) {
                LOGGER.error("QUERY_STAFF_LIST_YB_MONTH error storeCode:"+ storeManDto.getStoreCode() + ",month" + month+ ",cateCode" + cateCode ,e);
            }
            
            List<Map<String, Object>> satffYbCurrSales = biKafkaRealTimeSaleService.getStoreYbStaffDataFromRedis(storeManDto.getStoreCode(),cateCode);
            Map<String, Map<String, Object>> satffYbCurrSaleMap = null;
            if(CollectionUtils.isNotEmpty(satffYbCurrSales)) {
                satffYbCurrSaleMap = new HashMap<String, Map<String,Object>>();
                for (Map<String, Object> from : satffYbCurrSales) {
                    if (null != from.get("salesPerson")) {
                        String key = String.valueOf(from.get("salesPerson"));
                        satffYbCurrSaleMap.put(key, from);
                    }
                }
               /*satffYbCurrSaleMap = Maps.uniqueIndex(satffYbCurrSales,
                        new Function<Map<String, Object>, String>() {
                    @Override
                    public String apply(Map<String, Object> from) {
                        if (null != from.get("salesPerson")) {
                            return  String.valueOf(from.get("salesPerson"));
                        } 
                        return "";
                    }
                });*/
            }
            List<BiStoreYbStaffDetailDto> straffYbSaleList = new ArrayList<BiStoreYbStaffDetailDto>();
            //设置人员详情
            if (CollectionUtils.isNotEmpty(ybMonthStaffList)) {
                for (BiStoreYbMonthDto item : ybMonthStaffList) {
                    BiStoreYbStaffDetailDto staffDetail = new BiStoreYbStaffDetailDto();
                    if(item == null){
                        continue;
                    }
                    if (StringUtils.isNotBlank(item.getStaffId())) {
                        staffDetail.setStaffId(item.getStaffId());
                    } else{
                        continue;
                    }
                    
                    staffDetail.setStaffName(getStaffNameForYB(item.getStaffId()));
                    
                    //员工延保当日销售额
                    BigDecimal staffYbSaleAmount  = DJStringUtils.bigDecimal(item.getYbSaleAmount());
                    Map<String, Object> ybStaffCurrrData = biKafkaRealTimeSaleService.getStoreYbStaffDataFromRedis(storeManDto.getStoreCode(), cateCode,item.getStaffId());
                    BigDecimal staffYbSaleCurrAmount;
                    if (null == ybStaffCurrrData || null == ybStaffCurrrData.get("payAmt")) {
                        staffYbSaleCurrAmount = BigDecimal.ZERO;
                    } else {
                        staffYbSaleCurrAmount = DJStringUtils.bigDecimal(String.valueOf(ybStaffCurrrData.get("payAmt")));
                    }
                    staffYbSaleAmount = staffYbSaleAmount.add(staffYbSaleCurrAmount);
                    //延保员工信息销售额
                    //如果延保销售为空则忽略
                    if(BigDecimal.ZERO.compareTo(staffYbSaleAmount) == 0){
                        continue;
                    }

                    staffDetail.setStaffYbSaleAmount(BiSaleAmountDto.valueOf(staffYbSaleAmount,""));
                    staffDetail.setOrderParm(staffYbSaleAmount.toString());
                    if(satffYbCurrSaleMap != null && satffYbCurrSaleMap.containsKey(item.getStaffId())){
                        satffYbCurrSaleMap.remove(item.getStaffId());
                    }
                    straffYbSaleList.add(staffDetail);
                }
            }
            
            if (null != satffYbCurrSaleMap && !satffYbCurrSaleMap.isEmpty()) {
                Set<Entry<String, Map<String, Object>>> curSales = satffYbCurrSaleMap.entrySet();
                for(Entry<String, Map<String, Object>> entry : curSales){
                    Map<String, Object> item = entry.getValue();
                    BiStoreYbStaffDetailDto staffDetail = new BiStoreYbStaffDetailDto();
                    if (null != item.get("salesPerson")) {
                        staffDetail.setStaffId(String.valueOf(item.get("salesPerson")));
                    } else{
                        continue;
                    }
                    Employee employee = employeeService.queryEmployeeBasicInfo(String.valueOf(item.get("salesPerson")));
                    if(employee != null && StringUtils.isNotBlank(employee.getEmployeeName())){
                        staffDetail.setStaffName(employee.getEmployeeName());
                    }else{
                        staffDetail.setStaffName(String.valueOf(item.get("salesPerson")));
                    }
                    BigDecimal staffYbSaleAmount;
                    if (null != item.get("payAmt")) {
                        staffYbSaleAmount = DJStringUtils.bigDecimal(String.valueOf(item.get("payAmt")));
                    } else {
                        staffYbSaleAmount = BigDecimal.ZERO;
                    }
                    //延保品类销售额
                    //如果延保销售为空则忽略
                    if(BigDecimal.ZERO.compareTo(staffYbSaleAmount) == 0){
                        continue;
                    }
                    staffDetail.setStaffYbSaleAmount(BiSaleAmountDto.valueOf(staffYbSaleAmount,""));
                    staffDetail.setOrderParm(staffYbSaleAmount.toString());
                    straffYbSaleList.add(staffDetail);
                }
            }
            
            // 根据销售额排序
            Collections.sort(straffYbSaleList, new Comparator<BiStoreYbStaffDetailDto>() {
                @Override
                public int compare(BiStoreYbStaffDetailDto o1, BiStoreYbStaffDetailDto o2) {
                    if (o2 !=null && StringUtils.isNotBlank(o2.getOrderParm()) && o1 !=null && StringUtils.isNotBlank(o1.getOrderParm()) ) {
                        return DJStringUtils.bigDecimal(o2.getOrderParm()).compareTo(DJStringUtils.bigDecimal(o1.getOrderParm()));
                    } else if (o2 == null || StringUtils.isBlank(o2.getOrderParm())) {
                        return -1;
                    } else if (o1 == null || StringUtils.isBlank(o1.getOrderParm())) {
                        return 1;
                    }
                    // 降序
                    return 0;
                }
            });
      
            biStoreYbCateInfo.setList(straffYbSaleList);
            return biStoreYbCateInfo;
        } else {
            
            // 当月延保品类销售数据
            Date time = DateUtils.getDateByStr(month, "yyyy-MM");
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("storeCode", storeManDto.getStoreCode());
            paramMap.put("month", DateUtils.getDateStrByFormat(time, "yyyyMM"));
            paramMap.put("cateCode", cateCode);
            BiStoreYbDayDto ybCateSaleTotal = null;
            try {
                ybCateSaleTotal = dalClient.queryForObject("BI_STORE_YB.QUERY_CATE_YB_MONTH",
                        paramMap, BiStoreYbDayDto.class);
            } catch (Exception e) {
                LOGGER.error("QUERY_CATE_YB_MONTH error storeCode:"+ storeManDto.getStoreCode() + ",month" + month+ ",cateCode" + cateCode ,e);
            }
            
            // 当月品类销售额
            BigDecimal cateAmount;
            if (null == ybCateSaleTotal || StringUtils.isBlank(ybCateSaleTotal.getSaleAmount())) {
                cateAmount = BigDecimal.ZERO;
            } else {
                cateAmount = DJStringUtils.bigDecimal(ybCateSaleTotal.getSaleAmount());
            }
            
            
            //当月品类延保销售额
            BigDecimal ybCateAmount;
            if (null == ybCateSaleTotal || StringUtils.isBlank(ybCateSaleTotal.getYbSaleAmount())) {
                ybCateAmount = BigDecimal.ZERO;
            } else {
                ybCateAmount = DJStringUtils.bigDecimal(ybCateSaleTotal.getYbSaleAmount());
            }
            
            BiStoreYbCateInfo biStoreYbCateInfo = new BiStoreYbCateInfo();
            biStoreYbCateInfo.setYbSaleAmount(BiSaleAmountDto.valueOf(ybCateAmount));
            biStoreYbCateInfo.setYbRate(DJStringUtils.calculateRateString(ybCateAmount.toString(), cateAmount.toString()));
            
            List<SaleCategory> cateList = storeGuideBiService.getBlongGategoriesAll(storeManDto, cateCode);
            biStoreYbCateInfo.setCateList(cateList);
            
            // 当月延保品类员工销售列表
            List<BiStoreYbMonthDto> ybMonthStaffList = null;
            
            try {
                ybMonthStaffList = dalClient.queryForList("BI_STORE_YB.QUERY_STAFF_LIST_YB_MONTH",
                        paramMap, BiStoreYbMonthDto.class);
            } catch (Exception e) {
                LOGGER.error("QUERY_STAFF_LIST_YB_MONTH error storeCode:"+ storeManDto.getStoreCode() + ",month" + month+ ",cateCode" + cateCode ,e);
            }
            
            List<BiStoreYbStaffDetailDto> straffYbSaleList = new ArrayList<BiStoreYbStaffDetailDto>();
            // 数据不为空时
            if (CollectionUtils.isEmpty(ybMonthStaffList)) {
                biStoreYbCateInfo.setList(straffYbSaleList);
                return biStoreYbCateInfo;
            }
            //设置人员详情
            for (BiStoreYbMonthDto item : ybMonthStaffList) {
                BiStoreYbStaffDetailDto staffDetail = new BiStoreYbStaffDetailDto();
                if(item == null){
                    continue;
                }
                if (StringUtils.isNotBlank(item.getStaffId())) {
                    staffDetail.setStaffId(item.getStaffId());
                } else{
                    continue;
                }
                if(DJYB.equals(item.getStaffId()) ){
                    staffDetail.setStaffName(DJYB_NAME);
                }else{
                    Employee employee = employeeService.queryEmployeeBasicInfo(item.getStaffId());
                    if(employee != null && StringUtils.isNotBlank(employee.getEmployeeName())){
                        staffDetail.setStaffName(employee.getEmployeeName());
                    }else{
                        staffDetail.setStaffName(item.getStaffId());
                    }
                }
                //员工延保当日销售额
                BigDecimal staffYbSaleAmount  = DJStringUtils.bigDecimal(item.getYbSaleAmount());

                //延保员工信息销售额
                //如果延保销售为空则忽略
                if(BigDecimal.ZERO.compareTo(staffYbSaleAmount) == 0){
                    continue;
                }
                staffDetail.setStaffYbSaleAmount(BiSaleAmountDto.valueOf(staffYbSaleAmount,""));
                straffYbSaleList.add(staffDetail);
            }
            biStoreYbCateInfo.setList(straffYbSaleList);
            return biStoreYbCateInfo;
        }
    }

    @Override
    public BiStoreJxcInfo queryCateJxcByDate(String storeCode, String day) {
        LOGGER.info("enter queryCateJxcByDate, storeCode={},day={}", storeCode, day);
        
        // 判断是否是查询今天的销售排行
        if (DateUtils.getDateStrByFormat(null, "yyyy-MM-dd").equals(day)) {
            // 当日门店销售数据
            Map<String, Object> todaySale = biKafkaRealTimeSaleService.getStoreSaleDataFromRedis(storeCode);
            //当日门店销售额
            BigDecimal totalAmount;
            if (null == todaySale || null == todaySale.get("payAmt")) {
                totalAmount = BigDecimal.ZERO;
            } else {
                totalAmount = DJStringUtils.bigDecimal(String.valueOf(todaySale.get("payAmt")));
            }
          //当日门店进销差销售额
            BigDecimal jxcAmount;
            if (null == todaySale || null == todaySale.get("jxc_amount")) {
                jxcAmount = BigDecimal.ZERO;
            } else {
                jxcAmount = DJStringUtils.bigDecimal(String.valueOf(todaySale.get("jxc_amount")));
            }
            
            // 门店去年同期销售金额
            Map<String, Object> lasYearSale = biKafkaRealTimeSaleService.queryLastYearStoreSaleAmountEx(storeCode);
            
            //去年同期门店进销差销售额
            BigDecimal lastYearJxcAmount;
            if (null == lasYearSale || null == lasYearSale.get("JXC_AMOUNT")) {
                lastYearJxcAmount = BigDecimal.ZERO;
            } else {
                lastYearJxcAmount = DJStringUtils.bigDecimal(String.valueOf(lasYearSale.get("JXC_AMOUNT")));
            }
            
            BiStoreJxcInfo biStoreJxcInfo = new BiStoreJxcInfo();
            //门店进销差销售额
            biStoreJxcInfo.setJxcAmount(BiSaleAmountDto.valueOf(jxcAmount));
            //门店销售额
            biStoreJxcInfo.setSaleAmount(BiSaleAmountDto.valueOf(totalAmount));
            //进销差率
            biStoreJxcInfo.setSaleJxcRate(DJStringUtils.calculateRateString(jxcAmount.toString(), totalAmount.toString()));
            //进销差同比
            biStoreJxcInfo.setSaleRate(DJStringUtils.calculateYoyString(jxcAmount, lastYearJxcAmount));
            
            // 查询门店品类实时销售数据
            List<Map<String, Object>> cateSales = biKafkaRealTimeSaleService.getStoreCateSaleDataFromRedis(storeCode);
            List<BiStoreJxcCateDetail> resultList = new ArrayList<BiStoreJxcCateDetail>();
            // 数据不为空时
            if (CollectionUtils.isEmpty(cateSales)) {
                biStoreJxcInfo.setList(resultList);
                return biStoreJxcInfo;
            }
            // 根据销售额排序
            Collections.sort(cateSales, new Comparator<Map<String, Object>>() {
                @Override
                public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                    if (null != o2.get("jxc_amount") && null != o1.get("jxc_amount")) {
                        return DJStringUtils.bigDecimal(String.valueOf(o2.get("jxc_amount"))).compareTo(DJStringUtils.bigDecimal(String.valueOf(o1.get("jxc_amount"))));
                    } else if (null == o2.get("jxc_amount")) {
                        return -1;
                    } else if (null == o1.get("jxc_amount")) {
                        return 1;
                    }
                    // 降序
                    return 0;
                }
            });
            
            //获取去年同期品类销售列表，并转化成MAP
            List<Map<String, Object>> lastYearCateSaleList = biKafkaRealTimeSaleService.queryLastYearStoreSaleCateList(storeCode);
            Map<String, Map<String, Object>> lastYearCateSaleMap = Maps.uniqueIndex(lastYearCateSaleList,
                    new Function<Map<String, Object>, String>() {
                        @Override
                        public String apply(Map<String, Object> from) {
                            if(from.get("CATE_CODE") != null){
                                return String.valueOf(from.get("CATE_CODE"));
                            }else{
                                return "";
                            }
                        }
                    });
            
            //设置品类详情
            for (Map<String, Object> sale : cateSales) {
                // 获取品类编码
                String cateCode = String.valueOf(sale.get("deptCd"));
                String cateName = ConfigConstants.DEFAULT_CATE_MAP.get(cateCode);
                // 判断品类编码和品类名称是否为空
                if (StringUtils.isBlank(cateCode) || StringUtils.isBlank(cateName)) {
                    // 如果品类编码,或者品类名称不存在,则不处理该品类信息
                    continue;
                }
                BiStoreJxcCateDetail cateDetail = new BiStoreJxcCateDetail();
                cateDetail.setCateCode(cateCode);
                cateDetail.setCateName(cateName);
                //品类销售额
                BigDecimal cateSaleAmount;
                if (null != sale.get("payAmt")) {
                    cateSaleAmount = DJStringUtils.bigDecimal(String.valueOf(sale.get("payAmt")));
                } else {
                    cateSaleAmount = BigDecimal.ZERO;
                }
                
                //品类进销差销售额
                BigDecimal cateJxcAmount;
                if (null != sale.get("jxc_amount")) {
                    cateJxcAmount = DJStringUtils.bigDecimal(String.valueOf(sale.get("jxc_amount")));
                } else {
                    cateJxcAmount = BigDecimal.ZERO;
                }
                
                //去年同期品类进销差
                Map<String, Object> lastYearCateSale =  lastYearCateSaleMap.get(cateCode);
                BigDecimal lastYearCateJxcAmount;
                if (lastYearCateSale != null && null != lastYearCateSale.get("JXC_AMOUNT")) {
                    lastYearCateJxcAmount = DJStringUtils.bigDecimal(String.valueOf(lastYearCateSale.get("JXC_AMOUNT")));
                } else {
                    lastYearCateJxcAmount = BigDecimal.ZERO;
                }
                
                //延保品类销售额
                cateDetail.setCateSaleAmount(BiSaleAmountDto.valueOf(cateSaleAmount));
                cateDetail.setCateJxcAmount(BiSaleAmountDto.valueOf(cateJxcAmount));
                // 进销差率
                cateDetail.setCateJxcRate(DJStringUtils.calculateRateString(cateJxcAmount.toString(), cateSaleAmount.toString()));
                //进销差同比
                cateDetail.setCateRate(DJStringUtils.calculateYoyString(cateJxcAmount, lastYearCateJxcAmount));
                
                resultList.add(cateDetail);
            }
            biStoreJxcInfo.setList(resultList);
            return biStoreJxcInfo;
        } else {
            //日销售数据
            Date time = DateUtils.getDateByStr(day, "yyyy-MM-dd");
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("storeCode", storeCode);
            paramMap.put("day", DateUtils.getDateStrByFormat(time, "yyyyMMdd"));
            BiStoreSaleDayDto totalSale = null;
            try {
                totalSale = dalClient.queryForObject("BI_STORE_DATA_WAP.QUERY_STORE_AMOUNT_BY_DAY_EX",
                        paramMap, BiStoreSaleDayDto.class);
            } catch (Exception e) {
                LOGGER.error("QUERY_STORE_AMOUNT_BY_DAY_EX error storeCode:"+ storeCode+ ",day" + day ,e);
            }
            //门店日总销售额
            BigDecimal totalAmount;
            if (null == totalSale || StringUtils.isBlank(totalSale.getSaleAmount())) {
                totalAmount = BigDecimal.ZERO;
            } else {
                totalAmount = DJStringUtils.bigDecimal(totalSale.getSaleAmount());
            }
            
            //门店日进销差销售额
            BigDecimal jxcAmount;
            if (null == totalSale || StringUtils.isBlank(totalSale.getJxcAmount())) {
                jxcAmount = BigDecimal.ZERO;
            } else {
                jxcAmount = DJStringUtils.bigDecimal(totalSale.getJxcAmount());
            }
            
            //门店日进销差销售额
            BigDecimal lastYearJxcAmount;
            if (null == totalSale || StringUtils.isBlank(totalSale.getLastYearJxcAmount())) {
                lastYearJxcAmount = BigDecimal.ZERO;
            } else {
                lastYearJxcAmount = DJStringUtils.bigDecimal(totalSale.getLastYearJxcAmount());
            }
            
            BiStoreJxcInfo biStoreJxcInfo = new BiStoreJxcInfo();
            //门店进销差销售额
            biStoreJxcInfo.setJxcAmount(BiSaleAmountDto.valueOf(jxcAmount));
            //门店销售额
            biStoreJxcInfo.setSaleAmount(BiSaleAmountDto.valueOf(totalAmount));
            //进销差率
            biStoreJxcInfo.setSaleJxcRate(DJStringUtils.calculateRateString(jxcAmount.toString(), totalAmount.toString()));
            //进销差同比
            biStoreJxcInfo.setSaleRate(DJStringUtils.calculateYoyString(jxcAmount, lastYearJxcAmount));
            
            //日品类销售列表
            List<BiStoreCateSaleDayDto> cateSaleList = null;
            try {
                cateSaleList = dalClient.queryForList("BI_STORE_DATA_WAP.QUERY_STORE_CATE_SALE_DAY_LIST",
                        paramMap, BiStoreCateSaleDayDto.class);
            } catch (Exception e) {
                LOGGER.error("QUERY_STORE_CATE_SALE_DAY_LIST error storeCode:"+ storeCode+ ",day" + day ,e);
            }
            
            List<BiStoreJxcCateDetail> resultList = new ArrayList<BiStoreJxcCateDetail>();
            if(CollectionUtils.isEmpty(cateSaleList)){
                biStoreJxcInfo.setList(resultList);
                return biStoreJxcInfo;
            }
            
            for(BiStoreCateSaleDayDto item  : cateSaleList){
                // 获取品类编码
                String cateCode = item.getCateCode();
                String cateName = ConfigConstants.DEFAULT_CATE_MAP.get(cateCode);
                // 判断品类编码和品类名称是否为空
                if ( StringUtils.isBlank(cateCode) || StringUtils.isBlank(cateName)) {
                    // 如果品类编码,或者品类名称不存在,则不处理该品类信息
                    continue;
                }
                
                BiStoreJxcCateDetail cateDetail = new BiStoreJxcCateDetail();
                cateDetail.setCateCode(cateCode);
                cateDetail.setCateName(cateName);
                //品类销售额
                BigDecimal cateSaleAmount;
                if (null == item || StringUtils.isBlank(item.getSaleAmount())) {
                    cateSaleAmount = BigDecimal.ZERO;
                } else {
                    cateSaleAmount = DJStringUtils.bigDecimal(item.getSaleAmount());
                }
                
                //品类进销差销售额
                BigDecimal cateJxcAmount;
                if (null == item || StringUtils.isBlank(item.getJxcAmount())) {
                    cateJxcAmount = BigDecimal.ZERO;
                } else {
                    cateJxcAmount = DJStringUtils.bigDecimal(item.getJxcAmount());
                }
                
                //去年同期品类进销差
                BigDecimal lastYearCateJxcAmount;
                if (null == item || StringUtils.isBlank(item.getLastYearJxcAmount())) {
                    lastYearCateJxcAmount = BigDecimal.ZERO;
                } else {
                    lastYearCateJxcAmount = DJStringUtils.bigDecimal(item.getLastYearJxcAmount());
                }
                
                //延保品类销售额
                cateDetail.setCateSaleAmount(BiSaleAmountDto.valueOf(cateSaleAmount));
                cateDetail.setCateJxcAmount(BiSaleAmountDto.valueOf(cateJxcAmount));
                // 进销差率
                cateDetail.setCateJxcRate(DJStringUtils.calculateRateString(cateJxcAmount.toString(), cateSaleAmount.toString()));
                //进销差同比
                cateDetail.setCateRate(DJStringUtils.calculateYoyString(cateJxcAmount, lastYearCateJxcAmount));
                
                resultList.add(cateDetail);
            }
            biStoreJxcInfo.setList(resultList);
            return biStoreJxcInfo;
        }
    
    }

    @Override
    public BiStoreJxcInfo queryCateJxcByMonth(String storeCode, String month) {
        LOGGER.info("enter queryCateJxcByMonth, storeCode={},day={}", storeCode, month);
        
        // 判断是否是查询当月
        if (DateUtils.getDateStrByFormat(null, "yyyy-MM").equals(month)) {
            //当日门店销售额
            BigDecimal totalAmount;
          //当日门店进销差销售额
            BigDecimal jxcAmount;
            
            //月销售数据
            Date time = DateUtils.getDateByStr(month, "yyyy-MM");
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("storeCode", storeCode);
            paramMap.put("month", DateUtils.getDateStrByFormat(time, "yyyyMM"));
            BiStoreSaleDayDto totalSale = null;
            try {
                totalSale = dalClient.queryForObject("BI_STORE_DATA_WAP.QUERY_STORE_AMOUNT_BY_MONTH",
                        paramMap, BiStoreSaleDayDto.class);
            } catch (Exception e) {
                LOGGER.error("QUERY_STORE_AMOUNT_BY_MONTH error storeCode:"+ storeCode+ ",month" + month ,e);
            }
            
            //门店月总销售额
            if (null == totalSale || StringUtils.isBlank(totalSale.getSaleAmount())) {
                totalAmount = BigDecimal.ZERO;
            } else {
                totalAmount =DJStringUtils.bigDecimal(totalSale.getSaleAmount());
            }
            
            //门店月进销差销售额
            if (null == totalSale || StringUtils.isBlank(totalSale.getJxcAmount())) {
                jxcAmount = BigDecimal.ZERO;
            } else {
                jxcAmount = DJStringUtils.bigDecimal(totalSale.getJxcAmount());
            }
            
            //去年同期门店进销差销售额
            BigDecimal lastYearJxcAmount;
            if (null == totalSale || StringUtils.isBlank(totalSale.getLastYearJxcAmount())) {
                lastYearJxcAmount = BigDecimal.ZERO;
            } else {
                lastYearJxcAmount = DJStringUtils.bigDecimal(totalSale.getLastYearJxcAmount());
            }
            
            BiStoreJxcInfo biStoreJxcInfo = new BiStoreJxcInfo();
            //门店进销差销售额
            biStoreJxcInfo.setJxcAmount(BiSaleAmountDto.valueOf(jxcAmount));
            //门店销售额
            biStoreJxcInfo.setSaleAmount(BiSaleAmountDto.valueOf(totalAmount));
            //进销差率
            biStoreJxcInfo.setSaleJxcRate(DJStringUtils.calculateRateString(jxcAmount.toString(), totalAmount.toString()));
            //进销差同比
            biStoreJxcInfo.setSaleRate(DJStringUtils.calculateYoyString(jxcAmount, lastYearJxcAmount));
            
  
            //获取本月品类销售列表，并转化成MAP
            List<BiStoreCateSaleDayDto> cateSaleList =null;
            try {
                cateSaleList = dalClient.queryForList("BI_STORE_DATA_WAP.QUERY_STORE_CATE_SALE_MONTH_LIST",
                        paramMap, BiStoreCateSaleDayDto.class);
            } catch (Exception e) {
                LOGGER.error("QUERY_STORE_CATE_SALE_MONTH_LIST error storeCode:"+ storeCode+ ",month" + month ,e);
            }
            
            List<BiStoreJxcCateDetail> resultList = new ArrayList<BiStoreJxcCateDetail>();
            // 数据不为空时
            if (CollectionUtils.isEmpty(cateSaleList)) {
                biStoreJxcInfo.setList(resultList);
                return biStoreJxcInfo;
            }

            //设置品类详情
            for (BiStoreCateSaleDayDto sale : cateSaleList) {
                // 获取品类编码
                String cateCode = sale.getCateCode();
                String cateName = ConfigConstants.DEFAULT_CATE_MAP.get(cateCode);
                // 判断品类编码和品类名称是否为空
                if (StringUtils.isBlank(cateCode) || StringUtils.isBlank(cateName)) {
                    // 如果品类编码,或者品类名称不存在,则不处理该品类信息
                    continue;
                }
                BiStoreJxcCateDetail cateDetail = new BiStoreJxcCateDetail();
                cateDetail.setCateCode(cateCode);
                cateDetail.setCateName(cateName);
                
                BigDecimal lastYearCateJxcAmount;
                

                //去年同期进销差销售额
                if(StringUtils.isNotBlank(sale.getLastYearJxcAmount())){
                    lastYearCateJxcAmount =  DJStringUtils.bigDecimal(sale.getLastYearJxcAmount());
                }else{
                    lastYearCateJxcAmount = BigDecimal.ZERO;
                }
                
                //品类销售额
                BigDecimal cateSaleAmount;
                if(StringUtils.isNotBlank(sale.getSaleAmount())){
                    cateSaleAmount =  DJStringUtils.bigDecimal(sale.getSaleAmount());
                }else{
                    cateSaleAmount = BigDecimal.ZERO;
                }
                
                //品类进销差销售额
                BigDecimal cateJxcAmount;
                //进销差总销售额
                if(StringUtils.isNotBlank(sale.getJxcAmount())){
                    cateJxcAmount =  DJStringUtils.bigDecimal(sale.getJxcAmount());
                }else{
                    cateJxcAmount = BigDecimal.ZERO;
                }
            
                //延保品类销售额
                cateDetail.setCateSaleAmount(BiSaleAmountDto.valueOf(cateSaleAmount));
                cateDetail.setCateJxcAmount(BiSaleAmountDto.valueOf(cateJxcAmount));
                // 进销差率
                cateDetail.setCateJxcRate(DJStringUtils.calculateRateString(cateJxcAmount.toString(), cateSaleAmount.toString()));
                //进销差同比
                cateDetail.setCateRate(DJStringUtils.calculateYoyString(cateJxcAmount, lastYearCateJxcAmount));
                
                resultList.add(cateDetail);
            }
            biStoreJxcInfo.setList(resultList);
            return biStoreJxcInfo;
        } else {
            //月销售数据
            Date time = DateUtils.getDateByStr(month, "yyyy-MM");
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("storeCode", storeCode);
            paramMap.put("month", DateUtils.getDateStrByFormat(time, "yyyyMM"));
            BiStoreSaleDayDto totalSale = null;
            
            try {
                totalSale = dalClient.queryForObject("BI_STORE_DATA_WAP.QUERY_STORE_AMOUNT_BY_MONTH",
                        paramMap, BiStoreSaleDayDto.class);
            } catch (Exception e) {
                LOGGER.error("QUERY_STORE_AMOUNT_BY_MONTH error storeCode:"+ storeCode+ ",month" + month ,e);
            }
            
            //门店日总销售额
            BigDecimal totalAmount;
            if (null == totalSale || StringUtils.isBlank(totalSale.getSaleAmount())) {
                totalAmount = BigDecimal.ZERO;
            } else {
                totalAmount = DJStringUtils.bigDecimal(totalSale.getSaleAmount());
            }
            
            //门店日进销差销售额
            BigDecimal jxcAmount;
            if (null == totalSale || StringUtils.isBlank(totalSale.getJxcAmount())) {
                jxcAmount = BigDecimal.ZERO;
            } else {
                jxcAmount = DJStringUtils.bigDecimal(totalSale.getJxcAmount());
            }
            
            //门店同期日进销差销售额
            BigDecimal lastYearJxcAmount;
            if (null == totalSale || StringUtils.isBlank(totalSale.getLastYearJxcAmount())) {
                lastYearJxcAmount = BigDecimal.ZERO;
            } else {
                lastYearJxcAmount = DJStringUtils.bigDecimal(totalSale.getLastYearJxcAmount());
            }
            
            BiStoreJxcInfo biStoreJxcInfo = new BiStoreJxcInfo();
            //门店进销差销售额
            biStoreJxcInfo.setJxcAmount(BiSaleAmountDto.valueOf(jxcAmount));
            //门店销售额
            biStoreJxcInfo.setSaleAmount(BiSaleAmountDto.valueOf(totalAmount));
            //进销差率
            biStoreJxcInfo.setSaleJxcRate(DJStringUtils.calculateRateString(jxcAmount.toString(), totalAmount.toString()));
            //进销差同比
            biStoreJxcInfo.setSaleRate(DJStringUtils.calculateYoyString(jxcAmount, lastYearJxcAmount));
            
            // 数据不为空时
            List<BiStoreJxcCateDetail> resultList = new ArrayList<BiStoreJxcCateDetail>();

            //获取本月品牌销售列表，并转化成MAP
            List<BiStoreCateSaleDayDto> cateSaleList = null;
            try {
                cateSaleList = dalClient.queryForList("BI_STORE_DATA_WAP.QUERY_STORE_CATE_SALE_MONTH_LIST",
                        paramMap, BiStoreCateSaleDayDto.class);
            } catch (Exception e) {
                LOGGER.error("QUERY_STORE_CATE_SALE_MONTH_LIST error storeCode:"+ storeCode+ ",month" + month ,e);
            }
            if (CollectionUtils.isEmpty(cateSaleList)) {
                biStoreJxcInfo.setList(resultList);
                return biStoreJxcInfo;
            }
            
            //设置品类详情
            for (BiStoreCateSaleDayDto item : cateSaleList) {
                if(item == null){
                    continue;
                }
                // 获取品类编码
                String cateCode = item.getCateCode();
                String cateName = ConfigConstants.DEFAULT_CATE_MAP.get(cateCode);
                // 判断品类编码和品类名称是否为空
                if (StringUtils.isBlank(cateCode) || StringUtils.isBlank(cateName)) {
                    // 如果品类编码,或者品类名称不存在,则不处理该品类信息
                    continue;
                }
                BiStoreJxcCateDetail cateDetail = new BiStoreJxcCateDetail();
                cateDetail.setCateCode(cateCode);
                cateDetail.setCateName(cateName);
          
                //去年同期进销差销售额
                BigDecimal lastYearCateJxcAmount;
                if(StringUtils.isNotBlank(item.getLastYearJxcAmount())){
                    lastYearCateJxcAmount =  DJStringUtils.bigDecimal(item.getLastYearJxcAmount());
                }else{
                    lastYearCateJxcAmount = BigDecimal.ZERO;
                }
                
                //品类销售额
                BigDecimal cateSaleAmount;
                if(StringUtils.isNotBlank(item.getSaleAmount())){
                    cateSaleAmount =  DJStringUtils.bigDecimal(item.getSaleAmount());
                }else{
                    cateSaleAmount = BigDecimal.ZERO;
                }
                
                //品类进销差销售额
                BigDecimal cateJxcAmount;
                if(StringUtils.isNotBlank(item.getJxcAmount())){
                    cateJxcAmount =  DJStringUtils.bigDecimal(item.getJxcAmount());
                }else{
                    cateJxcAmount = BigDecimal.ZERO;
                }
            
                //销售额
                cateDetail.setCateSaleAmount(BiSaleAmountDto.valueOf(cateSaleAmount));
                cateDetail.setCateJxcAmount(BiSaleAmountDto.valueOf(cateJxcAmount));
                // 进销差率
                cateDetail.setCateJxcRate(DJStringUtils.calculateRateString(cateJxcAmount.toString(), cateSaleAmount.toString()));
                //进销差同比
                cateDetail.setCateRate(DJStringUtils.calculateYoyString(cateJxcAmount, lastYearCateJxcAmount));
                resultList.add(cateDetail);
            }
            biStoreJxcInfo.setList(resultList);
            return biStoreJxcInfo;
        }
    }

    /**
     * 查询单品详情
     * 
     * @param date 日查询YYYY-MM-DD，月查询YYYY-MM
     */
    @Override
    public CmmdtySaleDetailDto queryCmmdtySaleDetail(StoreManDto storeManDto, String cmmdtyCode, String date) {
        LOGGER.info("queryCmmdtySaleDetail. cmmdtyCode:{}, date:{}", cmmdtyCode, date);
        if (StringUtils.isBlank(cmmdtyCode) || StringUtils.isBlank(date)) {
            LOGGER.error("queryCmmdtySaleDetail param is null.");
            return new CmmdtySaleDetailDto();
        }
        return biCmmdtyService.queryCmmdtySaleDetail(storeManDto.getStoreCode(), cmmdtyCode, date);
    }
    
    /**
     * 
     * 功能描述: 日月维度单品销售列表<br>
     * 〈功能详细描述〉如果品类/品牌查询字段有值
     *
     * @param storeManDto
     * @param cateCode 督导身份，品类为必填项；店长为非必填
     * @param brandCode
     * @param cmmdtyCode
     * @param date 日查询YYYY-MM-DD，月查询YYYY-MM
     * @param sortType
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @Override
    public ReturnMsg<CmmdtySaleDetailsDto> queryStoreCmmdtySaleDetails(StoreManDto storeManDto, String cateCode,
            String brandCode, String cmmdtyCode, String date, String sortType, Integer pageNo, Integer pageSize) {
        LOGGER.info(
                "queryStoreCmmdtySaleDetails. storeManDto:{}, cateCode:{}, brandCode:{}, cmmdtyCode:{}, date:{}, sortType:{}",
                new Object[] {storeManDto, cateCode, brandCode, cmmdtyCode, date, sortType});
        ReturnMsg<CmmdtySaleDetailsDto> retMsg = new ReturnMsg<CmmdtySaleDetailsDto>();
        if (StringUtils.isBlank(date)) {
            LOGGER.error("queryStoreCmmdtySaleDetails. date is null");
            retMsg.setError(ErrorCodeConstants.ERR_0001);
            return retMsg;
        }
        // 如果是督导，并且商品编码为空，品类为必填
        if (ClerkConstants.SALE_GUIDE.equals(storeManDto.getPositionId())) {
            if (StringUtils.isBlank(cmmdtyCode) && StringUtils.isBlank(cateCode)) {
                LOGGER.error("queryStoreCmmdtySaleDetails. Guide`s cateCode is null");
                retMsg.setError(ErrorCodeConstants.ERR_0001);
                return retMsg;
            }
        }
        
        CmmdtySaleDetailsDto details = new CmmdtySaleDetailsDto();
        
        // 若商品编码有值，将品牌品类编码致空。防止前台参数异常。
        if (StringUtils.isNotBlank(cmmdtyCode)) {
            cateCode = null;
            brandCode = null;
        }

        // 品类信息、品牌信息、品牌总销售额、品牌总销售数量
        if (StringUtils.isNotBlank(brandCode)) {
            // 日数据
            if (date.length() == DAY_DATE_LENGTH) {
                String day = DateUtils.getDateStrByFormat(DateUtils.getDateByStr(date, DateUtils.DEFAULT_DATE_PATTERN),
                        DateUtils.YYYYMMDD_PATTERN);
                // 今日
                if (DateUtils.getDateStrByFormat(null, "yyyyMMdd").equals(day)) {
                    // 查询门店品牌实时销售数据
                    List<Map<String, Object>> todaybrandSales = biKafkaRealTimeSaleService
                            .getStoreBrandSaleDataByCateFromRedis(storeManDto.getStoreCode(), cateCode);
                    if (CollectionUtils.isNotEmpty(todaybrandSales)) {
                        for (Map<String, Object> sale : todaybrandSales) {
                            if (StringUtils.isBlank(String.valueOf(sale.get("brandCd")))
                                    || !brandCode.equals(String.valueOf(sale.get("brandCd")))) {
                                continue;
                            }
                            String brandCodeEx = String.valueOf(sale.get("brandCd"));
                            String brandName = String.valueOf(sale.get("brandName"));
                            String cateCodeEx = String.valueOf(sale.get("deptCd"));
                            String cateName = ConfigConstants.DEFAULT_CATE_MAP.get(cateCodeEx);
                            String totalSaleAmount = "0";
                            if (null != sale.get("payAmt")) {
                                totalSaleAmount = String.valueOf(sale.get("payAmt"));
                            }
                            String totalSaleCnt = "0";
                            if (null != sale.get("saleCnt")) {
                                totalSaleCnt = String.valueOf(sale.get("saleCnt"));
                            }
                            details.setCateCode(cateCodeEx);
                            details.setCateName(cateName);
                            details.setBrandCode(brandCodeEx);
                            details.setBrandName(brandName);
                            details.setTotalSaleAmount(BiSaleAmountDto.valueOf(totalSaleAmount));
                            details.setTotalSaleCnt(DJStringUtils.transIntergerStr(totalSaleCnt));
                            break;
                        }
                    }
                } else {
                    // 查询品牌
                    List<BiBrandSale> brandSales = biBrandDaoService.queryBrandSaleByDay(cateCode, brandCode, day,
                            storeManDto.getStoreCode(), ORDER_BY_AMOUNT);
                    if (CollectionUtils.isNotEmpty(brandSales)) {
                        for (BiBrandSale sale : brandSales) {
                            if (StringUtils.isBlank(sale.getBrandCode()) || !brandCode.equals(sale.getBrandCode())) {
                                continue;
                            }
                            String brandCodeEx = sale.getBrandCode();
                            String brandName = sale.getBrandName();
                            String cateCodeEx = sale.getCateCode();
                            String cateName = ConfigConstants.DEFAULT_CATE_MAP.get(cateCodeEx);
                            String totalSaleAmount = "0";
                            if (null != sale.getBrandAmount()) {
                                totalSaleAmount = String.valueOf(sale.getBrandAmount());
                            }
                            String totalSaleCnt = "0";
                            if (null != sale.getSaleCnt()) {
                                totalSaleCnt = String.valueOf(sale.getSaleCnt());
                            }
                            details.setCateCode(cateCodeEx);
                            details.setCateName(cateName);
                            details.setBrandCode(brandCodeEx);
                            details.setBrandName(brandName);
                            details.setTotalSaleAmount(BiSaleAmountDto.valueOf(totalSaleAmount));
                            details.setTotalSaleCnt(DJStringUtils.transIntergerStr(totalSaleCnt));
                            break;
                        }
                    }
                }
            } else {
                // 月数据
                String month = DateUtils.getDateStrByFormat(
                        DateUtils.getDateByStr(date, DateUtils.DEFAULT_MONTH_PATTERN), DateUtils.YYYYMM_PATTERN);
                // 查询月品牌信息
                List<BiBrandSale> brandSales = biBrandDaoService.queryBrandSaleByMonth(cateCode, brandCode, month,
                        storeManDto.getStoreCode(), ORDER_BY_AMOUNT);
                String cateCodeEx = "";
                String cateName = "";
                String brandCodeEx = "";
                String brandName = "";
                String totalSaleAmount = "0";
                String totalSaleCnt = "0";
                if (CollectionUtils.isNotEmpty(brandSales)) {
                    for (BiBrandSale sale : brandSales) {
                        if (StringUtils.isBlank(sale.getBrandCode()) || !brandCode.equals(sale.getBrandCode())) {
                            continue;
                        }
                        brandCodeEx = sale.getBrandCode();
                        brandName = sale.getBrandName();
                        cateCodeEx = sale.getCateCode();
                        cateName = ConfigConstants.DEFAULT_CATE_MAP.get(cateCodeEx);
                        if (null != sale.getBrandAmount()) {
                            totalSaleAmount = String.valueOf(sale.getBrandAmount());
                        }
                        if (null != sale.getSaleCnt()) {
                            totalSaleCnt = String.valueOf(sale.getSaleCnt());
                        }
                        break;
                    }
                }
                // 本月 加上实时数据
                if (DateUtils.getDateStrByFormat(null, "yyyyMM").equals(month)) {
                    // 查询门店品牌实时销售数据
                    List<Map<String, Object>> todaybrandSales = biKafkaRealTimeSaleService
                            .getStoreBrandSaleDataByCateFromRedis(storeManDto.getStoreCode(), cateCode);
                    if (CollectionUtils.isNotEmpty(todaybrandSales)) {
                        for (Map<String, Object> sale : todaybrandSales) {
                            String code = String.valueOf(sale.get("brandCd"));
                            if (StringUtils.isBlank(code) || !brandCode.equals(code)) {
                                continue;
                            }
                            if (null != sale.get("payAmt")) {
                                totalSaleAmount = String.valueOf(DJStringUtils.bigDecimal(totalSaleAmount).add(
                                        DJStringUtils.bigDecimal(String.valueOf(sale.get("payAmt")))));
                            }
                            if (null != sale.get("saleCnt")) {
                                totalSaleCnt = String.valueOf(DJStringUtils.bigDecimal(totalSaleCnt).add(
                                        DJStringUtils.bigDecimal(String.valueOf(sale.get("saleCnt")))));
                            }
                            break;
                        }
                    }
                }
                details.setCateCode(cateCodeEx);
                details.setCateName(cateName);
                details.setBrandCode(brandCodeEx);
                details.setBrandName(brandName);
                details.setTotalSaleAmount(BiSaleAmountDto.valueOf(totalSaleAmount));
                details.setTotalSaleCnt(DJStringUtils.transIntergerStr(totalSaleCnt));
            }
        }
        // 品类信息、品类总销售额、品类总销售数量
        else if (StringUtils.isNotBlank(cateCode)) {
            // 日数据
            if (date.length() == DAY_DATE_LENGTH) {
                String day = DateUtils.getDateStrByFormat(DateUtils.getDateByStr(date, DateUtils.DEFAULT_DATE_PATTERN),
                        DateUtils.YYYYMMDD_PATTERN);
                // 今日
                if (DateUtils.getDateStrByFormat(null, "yyyyMMdd").equals(day)) {
                    // 查询门店八大品类实时销售数据
                    List<Map<String, Object>> todayCateSales = biKafkaRealTimeSaleService
                            .getStoreCateSaleDataFromRedis(storeManDto.getStoreCode());
                    if (CollectionUtils.isNotEmpty(todayCateSales)) {
                        for (Map<String, Object> sale : todayCateSales) {
                            if (StringUtils.isBlank(String.valueOf(sale.get("deptCd")))
                                    || !cateCode.equals(String.valueOf(sale.get("deptCd")))) {
                                continue;
                            }
                            String cateCodeEx = String.valueOf(sale.get("deptCd"));
                            String cateName = ConfigConstants.DEFAULT_CATE_MAP.get(cateCodeEx);
                            String totalSaleAmount = "0";
                            if (null != sale.get("payAmt")) {
                                totalSaleAmount = String.valueOf(sale.get("payAmt"));
                            }
                            String totalSaleCnt = "0";
                            if (null != sale.get("saleCnt")) {
                                totalSaleCnt = String.valueOf(sale.get("saleCnt"));
                            }
                            details.setCateCode(cateCodeEx);
                            details.setCateName(cateName);
                            details.setTotalSaleAmount(BiSaleAmountDto.valueOf(totalSaleAmount));
                            details.setTotalSaleCnt(DJStringUtils.transIntergerStr(totalSaleCnt));
                            break;
                        }
                    }
                } else {
                    Map<String, Object> paramMap = new HashMap<String, Object>();
                    paramMap.put("storeCode", storeManDto.getStoreCode());
                    paramMap.put("day", day);
                    paramMap.put("cates", cateCode);
                    List<BiStoreCateSaleDataDayDto> cateSales = dalClient
                            .queryForList("BI_STORE_DATA_WAP.QUERY_STORE_CATE_DATA_BY_DAY", paramMap,
                                    BiStoreCateSaleDataDayDto.class);
                    if (CollectionUtils.isNotEmpty(cateSales)) {
                        for (BiStoreCateSaleDataDayDto sale : cateSales) {
                            if (StringUtils.isBlank(sale.getCateCode()) || !cateCode.equals(sale.getCateCode())) {
                                continue;
                            }
                            String cateCodeEx = sale.getCateCode();
                            String cateName = ConfigConstants.DEFAULT_CATE_MAP.get(cateCodeEx);
                            if (StringUtils.isBlank(cateName)) {
                                continue;
                            }
                            String totalSaleAmount = "0";
                            if (StringUtils.isNotBlank(sale.getSaleAmount())) {
                                totalSaleAmount = sale.getSaleAmount();
                            }
                            String totalSaleCnt = "0";
                            if (StringUtils.isNotBlank(sale.getSaleVolume())) {
                                totalSaleCnt = sale.getSaleVolume();
                            }
                            details.setCateCode(cateCodeEx);
                            details.setCateName(cateName);
                            details.setTotalSaleAmount(BiSaleAmountDto.valueOf(totalSaleAmount));
                            details.setTotalSaleCnt(DJStringUtils.transIntergerStr(totalSaleCnt));
                            break;
                        }
                    }
                }
            } else {
                // 月数据
                String month = DateUtils.getDateStrByFormat(
                        DateUtils.getDateByStr(date, DateUtils.DEFAULT_MONTH_PATTERN), DateUtils.YYYYMM_PATTERN);
                // 查询月份品类数据
                Map<String, Object> paramMap = new HashMap<String, Object>();
                paramMap.put("month", month);
                paramMap.put("storeCode", storeManDto.getStoreCode());
                List<BiStoreCateSaleDayDto> saleMonths = dalClient.queryForList(
                        "BI_STORE_DATA_WAP.QUERY_STORE_CATE_AMOUNT_RANK_MONTH", paramMap, BiStoreCateSaleDayDto.class);
                String cateCodeEx = "";
                String cateName = "";
                String totalSaleAmount = "0";
                String totalSaleCnt = "0";
                if (CollectionUtils.isNotEmpty(saleMonths)) {
                    for (BiStoreCateSaleDayDto sale : saleMonths) {
                        if (StringUtils.isBlank(sale.getCateCode()) || !cateCode.equals(sale.getCateCode())) {
                            continue;
                        }
                        cateCodeEx = sale.getCateCode();
                        cateName = ConfigConstants.DEFAULT_CATE_MAP.get(cateCodeEx);
                        if (StringUtils.isNotBlank(sale.getSaleAmount())) {
                            totalSaleAmount = sale.getSaleAmount();
                        }
                        if (StringUtils.isNotBlank(sale.getSaleVolume())) {
                            totalSaleCnt = sale.getSaleVolume();
                        }
                        break;
                    }
                }
                // 本月 加上今日数据
                if (DateUtils.getDateStrByFormat(null, "yyyyMM").equals(month)) {
                    // 查询门店八大品类实时销售数据
                    List<Map<String, Object>> todayCateSales = biKafkaRealTimeSaleService
                            .getStoreCateSaleDataFromRedis(storeManDto.getStoreCode());
                    if (CollectionUtils.isNotEmpty(todayCateSales)) {
                        for (Map<String, Object> sale : todayCateSales) {
                            String code = String.valueOf(sale.get("deptCd"));
                            if (StringUtils.isBlank(code) || !cateCode.equals(code)) {
                                continue;
                            }
                            if (null != sale.get("payAmt")) {
                                totalSaleAmount = String.valueOf(DJStringUtils.bigDecimal(totalSaleAmount).add(
                                        DJStringUtils.bigDecimal(String.valueOf(sale.get("payAmt")))));
                            }
                            if (null != sale.get("saleCnt")) {
                                totalSaleCnt = String.valueOf(DJStringUtils.bigDecimal(totalSaleCnt).add(
                                        DJStringUtils.bigDecimal(String.valueOf(sale.get("saleCnt")))));
                            }
                            break;
                        }
                    }
                }
                details.setCateCode(cateCodeEx);
                details.setCateName(cateName);
                details.setTotalSaleAmount(BiSaleAmountDto.valueOf(totalSaleAmount));
                details.setTotalSaleCnt(DJStringUtils.transIntergerStr(totalSaleCnt));
            }
        }
        
        // 170621，该版本不用考虑品类品牌数据，直接查询单品列表接口即可。
        CmmdtySaleDetailsDto cmmdtyDetails = biCmmdtyService.queryCmmdtySaleList(storeManDto.getStoreCode(),
                cateCode, brandCode, cmmdtyCode, sortType, date, pageNo, pageSize);
        details.setTotalSize(cmmdtyDetails.getTotalSize());
        details.setCmmdyList(cmmdtyDetails.getCmmdyList());
        // 上层查到数据或者商品编码不为空或者都为空，再查询单品列表
//        if (StringUtils.isNotBlank(details.getCateCode()) || StringUtils.isNotBlank(details.getBrandCode())
//                || StringUtils.isNotBlank(cmmdtyCode)
//                || (StringUtils.isBlank(cateCode) && StringUtils.isBlank(brandCode) && StringUtils.isBlank(cmmdtyCode))) {
//            CmmdtySaleDetailsDto cmmdtyDetails = biCmmdtyService.queryCmmdtySaleList(storeManDto.getStoreCode(),
//                    cateCode, brandCode, cmmdtyCode, sortType, date, pageNo, pageSize);
//            details.setTotalSize(cmmdtyDetails.getTotalSize());
//            details.setCmmdyList(cmmdtyDetails.getCmmdyList());
//        }

        retMsg.setData(details);

        return retMsg;
    }

    /**
     * 
     * 功能描述: 查询topN品牌列表<br>
     * 〈功能详细描述〉
     *
     * @param storeManDto
     * @param cateCode 
     * @param date 日期格式YYYY-MM-DD、YYYY-MM
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    @Override
    public ReturnMsg<List<TopCateDto>> queryTopNBrandList(StoreManDto manDto, String cateCode, String date) {
        LOGGER.info("queryTopBrandList. manDto:{}, cateCode:{}, date:{}", new Object[] {manDto, cateCode, date});
        ReturnMsg<List<TopCateDto>> returnMsg = new ReturnMsg<List<TopCateDto>>();
        List<TopCateDto> topCateList = new ArrayList<TopCateDto>();
        List<String> cateList = new ArrayList<String>();
        if (StringUtils.isBlank(date) || null == manDto) {
            LOGGER.error("queryTopBrandList. param is null");
            returnMsg.setError(ErrorCodeConstants.ERR_0001);
            return returnMsg;
        }
        // 若是督导，获取名下品类的品牌
        if (ClerkConstants.SALE_GUIDE.equals(manDto.getPositionId())) {
            // 督导的品类为必填项
            if (StringUtils.isBlank(cateCode)) {
                LOGGER.error("queryTopBrandList. Guide cateCode is null");
                returnMsg.setError(ErrorCodeConstants.ERR_0001);
                return returnMsg;
            }
            if (CollectionUtils.isNotEmpty(manDto.getCategories())) {
                for (Category cg : manDto.getCategories()) {
                    cateList.add(cg.getCategoryCode());
                }
            }
        }
        if (ClerkConstants.STORE_KEEPER.equals(manDto.getPositionId())
                || ClerkConstants.DEPUTY_STORE_KEEPER.equals(manDto.getPositionId())) {
            cateList.addAll(ConfigConstants.DEFAULT_CATE_MAP.keySet());
        }

        if (CollectionUtils.isEmpty(cateList)) {
            returnMsg.setData(topCateList);
            return returnMsg;
        }

        for (String cate : cateList) {
            TopCateDto topCate = new TopCateDto();
            topCate.setCateCode(cate);
            topCate.setCateName(ConfigConstants.DEFAULT_CATE_MAP.get(cate));
            topCate.setChooseStatus(cateCode.equals(cate) ? SaleCategory.ChooseStatus.YES
                    : SaleCategory.ChooseStatus.NO);
            topCate.setBrandList(biCmmdtyService.queryTopNBrandList(manDto.getStoreCode(), cate, date));
            topCateList.add(topCate);
        }

        returnMsg.setData(topCateList);

        return returnMsg;
    }

    /**
     * 查询平均单价
     */
    @Override
    public ReturnMsg<CmmdtyAvgPriceDto> queryCmmdtyAvgPrice(StoreManDto storeManDto, String brandCode, String date,
            Integer pageSize, Integer pageNo) {
        LOGGER.info("queryCmmdtyAvgPrice.brandCode:{}, date:{}", brandCode, date);
        ReturnMsg<CmmdtyAvgPriceDto> returnMsg = new ReturnMsg<CmmdtyAvgPriceDto>();
        CmmdtyAvgPriceDto dto = new CmmdtyAvgPriceDto();

        if (StringUtils.isBlank(date) || StringUtils.isBlank(brandCode)) {
            LOGGER.error("queryCmmdtyAvgPrice. param is null");
            returnMsg.setError(ErrorCodeConstants.ERR_0001);
            return returnMsg;
        }

        // 查询品牌数据(含品类)
        List<BiBrandSale> brandSales;
        // 日维度YYYY-MM-DD
        if (date.length() == DAY_DATE_LENGTH) {
            // 今日 查询品牌
            if (DateUtils.getDateStrByFormat(null, "yyyy-MM-dd").equals(date)) {
                // 查询品牌实时数据
                brandSales = biBrandService.getBrandSaleByDay(null, ORDER_BY_AMOUNT, storeManDto.getStoreCode());
            } else {
                // 非今日 查询品牌
                brandSales = biBrandDaoService.queryBrandSaleByDay(null, brandCode, DateUtils.getDateStrByFormat(
                        DateUtils.getDateByStr(date, DateUtils.DEFAULT_DATE_PATTERN), DateUtils.YYYYMMDD_PATTERN),
                        storeManDto.getStoreCode(), ORDER_BY_AMOUNT);
            }
        } else {
            // 月维度YYYY-MM 查询月品牌信息
            brandSales = biBrandDaoService.queryBrandSaleByMonth(null, brandCode, DateUtils.getDateStrByFormat(
                    DateUtils.getDateByStr(date, DateUtils.DEFAULT_MONTH_PATTERN), DateUtils.YYYYMM_PATTERN),
                    storeManDto.getStoreCode(), ORDER_BY_AMOUNT);
        }

        String cateCodeIn = "";
        String cateNameIn = "";
        String brandCodeIn = "";
        String brandNameIn = "";
        String brandSaleMoneyIn = "0";
        String brandSaleCntIn = "";
        String brandAvgPriceIn = "0";
        String avgPriceRateIn = "0.0";
        if (CollectionUtils.isNotEmpty(brandSales)) {
            for (BiBrandSale sale : brandSales) {
                if (null == sale || StringUtils.isBlank(sale.getBrandCode()) || !brandCode.equals(sale.getBrandCode())) {
                    continue;
                }
                brandCodeIn = sale.getBrandCode();
                brandNameIn = sale.getBrandName();
                cateCodeIn = sale.getCateCode();
                cateNameIn = ConfigConstants.DEFAULT_CATE_MAP.get(cateCodeIn);
                brandSaleMoneyIn = String.valueOf(sale.getBrandAmount());
                brandSaleCntIn = String.valueOf(sale.getSaleCnt());
                brandAvgPriceIn = String.valueOf(DJStringUtils.divide(sale.getBrandAmount(), sale.getSaleCnt()));
                avgPriceRateIn = "0.0";// 平均单价同比
                if (null != sale.getBrandAmountLy() && null != sale.getBrandCntLy()) {
                    BigDecimal brandAvgPriceInLy = DJStringUtils.divide(sale.getBrandAmountLy(), sale.getBrandCntLy());
                    avgPriceRateIn = DJStringUtils.calculateYoyString(DJStringUtils.bigDecimal(brandAvgPriceIn),
                            brandAvgPriceInLy);
                }
                break;
            }
        }
        
        // 品牌或者品类查询到数据才查询单品列表
        if (StringUtils.isNotBlank(cateCodeIn) || StringUtils.isNotBlank(brandCodeIn)) {
            // 单品平均单价列表
            CmmdtyAvgPriceDto cmmdtyDto = biCmmdtyService.queryCmmdtyAvgPriceList(storeManDto.getStoreCode(),
                    brandCode, date, pageNo, pageSize);
            dto.setTotalSize(cmmdtyDto.getTotalSize());
            dto.setCmmdtyAvgPriceList(cmmdtyDto.getCmmdtyAvgPriceList());
        }
        
        dto.setCateCode(cateCodeIn);
        dto.setCateName(cateNameIn);
        dto.setBrandCode(brandCodeIn);
        dto.setBrandName(brandNameIn);
        dto.setBrandSaleMoney(BiSaleAmountDto.valueOf(brandSaleMoneyIn));
        dto.setBrandSaleCnt(brandSaleCntIn);
        dto.setBrandAvgPrice(BiSaleAmountDto.valueOfLow(DJStringUtils.bigDecimal(brandAvgPriceIn)));
        dto.setAvgPriceRate(avgPriceRateIn);

        returnMsg.setData(dto);

        return returnMsg;
    }
    
}
